DvaJS入门解析

最近找了个RN+TS仿喜马拉雅的项目,看到dva那几节蚌埠住了,然后就去找了个网课看看,写篇博客总结一下

一.什么是Dva

  dva = React-Router + Redux + Redux-saga

二.安装

1.安装 dva-cli
npm install dva-cli -g
2.扎到安装项目的目录
cd ylz_project/my_reactdemo
3.创建项目:Dva-test项目名
dva new Dva-test
4.进入项目
cd Dva-test
5.启动项目
npm  start

 

三.项目结构

├── /dist/                                         // 打包目标目录
├── /src/                                          // 项目源码目录
│ ├── /components/                                 // 通用组件目录
│ ├── /models/                                     // 数据模型 
│ 				└── example.js                     // model example, dva的模型是一个集合了redux中 reducer 和 store,异步action等的抽象概念。
│ ├── /services/                                   // 存放 服务相关组件或函数
│ ├── /mock/                                       // mock(后端没给接口时测试用的假数据)
│ ├── /routes/                                     // 与路由对应的页面
│ 				└── page.js                        // 与路由规则匹配的页面组件
│ ├── index.css                                    // 项目入口css
│ ├── index.js                                     // 项目入口,手动配置开发时候开发的模块
│ └── router.js                                    // 项目路由 (默认使用React-Router中的HashRouter,所以你会看到URL最后有一个#号,可以通过使用dva-no-router禁用react-router)
├── package.json                                   // 项目依赖信息
├── .eslintrc                                      //  Eslint配置
├── .gitignore                                     //  git 忽略文件以及目录
└── .webpackrc                                     //  roadhog配置
└── README.md                                      //  开发文档

四.Dva概念

这部分主要来自官方文档 我就直接复制来了

数据流向

数据的改变发生通常是通过用户交互行为或者浏览器行为(如路由跳转等)触发的,当此类行为会改变数据的时候可以通过 dispatch 发起一个 action,如果是同步行为会直接通过 Reducers 改变 State ,如果是异步行为(副作用)会先触发 Effects 然后流向 Reducers 最终改变 State,所以在 dva 中,数据流向非常清晰简明,并且思路基本跟开源社区保持一致(也是来自于开源社区)。

Models 

model毫无疑问是dva中最重要的概念,这里的model是指领域模型,用于把数据相关的逻辑聚合到一起,几乎所有的数据,逻辑都在这边进行处理分发

 State

type State = any

State 表示 Model 的状态数据,通常表现为一个 javascript 对象(当然它可以是任何值);操作的时候每次都要当作不可变数据(immutable data)来对待,保证每次都是全新对象,没有引用关系,这样才能保证 State 的独立性,便于测试和追踪变化。

在 dva 中你可以通过 dva 的实例属性 _store 看到顶部的 state 数据,但是通常你很少会用到:

const app = dva();
console.log(app._store); // 顶部的 state 数据

Action

type AsyncAction = any

Action 是一个普通 javascript 对象,它是改变 State 的唯一途径。无论是从 UI 事件、网络回调,还是 WebSocket 等数据源所获得的数据,最终都会通过 dispatch 函数调用一个 action,从而改变对应的数据。action 必须带有 type 属性指明具体的行为,其它字段可以自定义,如果要发起一个 action 需要使用 dispatch 函数;需要注意的是 dispatch 是在组件 connect Models以后,通过 props 传入的。

dispatch({
  type: 'add',
});

dispatch函数 

type dispatch = (a: Action) => Action

dispatching function 是一个用于触发 action 的函数,action 是改变 State 的唯一途径,但是它只描述了一个行为,而 dipatch 可以看作是触发这个行为的方式,而 Reducer 则是描述如何改变数据的。

在 dva 中,connect Model 的组件通过 props 可以访问到 dispatch,可以调用 Model 中的 Reducer 或者 Effects,常见的形式如:

dispatch({
  type: 'user/add', // 如果在 model 外调用,需要添加 namespace
  payload: {}, // 需要传递的信息
});

Reducer 

type Reducer<S, A> = (state: S, action: A) => S

Reducer(也称为 reducing function)函数接受两个参数:之前已经累积运算的结果和当前要被累积的值,返回的是一个新的累积结果。该函数把一个集合归并成一个单值。

Reducer 的概念来自于是函数式编程,很多语言中都有 reduce API。如在 javascript 中:

[{x:1},{y:2},{z:3}].reduce(function(prev, next){
    return Object.assign(prev, next);
})
//return {x:1, y:2, z:3}

在 dva 中,reducers 聚合积累的结果是当前 model 的 state 对象。通过 actions 中传入的值,与当前 reducers 中的值进行运算获得新的值(也就是新的 state)。需要注意的是 Reducer 必须是纯函数,所以同样的输入必然得到同样的输出,它们不应该产生任何副作用。并且,每一次的计算都应该使用immutable data,这种特性简单理解就是每次操作都是返回一个全新的数据(独立,纯净),所以热重载和时间旅行这些功能才能够使用。

Effect

Effect 被称为副作用,在我们的应用中,最常见的就是异步操作。它来自于函数编程的概念,之所以叫副作用是因为它使得我们的函数变得不纯,同样的输入不一定获得同样的输出。

dva 为了控制副作用的操作,底层引入了redux-sagas做异步流程控制,由于采用了generator的相关概念,所以将异步转成同步写法,从而将effects转为纯函数。至于为什么我们这么纠结于 纯函数,如果你想了解更多可以阅读Mostly adequate guide to FP,或者它的中文译本JS函数式编程指南

Subscription

Subscriptions 是一种从  获取数据的方法,它来自于 elm。

Subscription 语义是订阅,用于订阅一个数据源,然后根据条件 dispatch 需要的 action。数据源可以是当前的时间、服务器的 websocket 连接、keyboard 输入、geolocation 变化、history 路由变化等等。

import key from 'keymaster';
...
app.model({
  namespace: 'count',
  subscriptions: {
    keyEvent({dispatch}) {
      key('⌘+up, ctrl+up', () => { dispatch({type:'add'}) });
    },
  }
});

Router

这里的路由通常指的是前端路由,由于我们的应用现在通常是单页应用,所以需要前端代码来控制路由逻辑,通过浏览器提供的 History API 可以监听浏览器url的变化,从而控制路由相关操作。

dva 实例提供了 router 方法来控制路由,使用的是react-router

import { Router, Route } from 'dva/router';
app.router(({history}) =>
  <Router history={history}>
    <Route path="/" component={HomePage} />
  </Router>
);

Route Components

组件设计方法中,我们提到过 Container Components,在 dva 中我们通常将其约束为 Route Components,因为在 dva 中我们通常以页面维度来设计 Container Components。

所以在 dva 中,通常需要 connect Model的组件都是 Route Components,组织在/routes/目录下,而/components/目录下则是纯组件(Presentational Components)。

五.Dva API

app = dva(opts)

创建应用,返回 dva 实例。(注:dva 支持多实例)

opts 包含:

  • history:指定给路由用的 history,默认是 hashHistory
  • initialState:指定初始数据,优先级高于 model 中的 state,默认是 {}

如果要配置 history 为 browserHistory,可以这样:

import createHistory from 'history/createBrowserHistory';
const app = dva({
  history: createHistory(),
});

另外,出于易用性的考虑,opts 里也可以配所有的 hooks ,下面包含全部的可配属性:

const app = dva({
  history,
  initialState,
  onError,
  onAction,
  onStateChange,
  onReducer,
  onEffect,
  onHmr,
  extraReducers,
  extraEnhancers,
});

剩下的太多了 懒得复制了 点这里可以看官方文档写的 

六.小例子

 

架构

 components/child.jsx

import React, { Component } from "react";
import { withRouter } from "react-router";

class Child extends Component {
  handleToIndex = () => {
    this.props.history.push("/");
  };

  render() {
    return (
      <div>
        <div>我是通用组件</div>
        <button onClick={this.handleToIndex}>首页_child</button>
      </div>
    );
  }
}

export default withRouter(Child);

models/indexTest.js

import * as apis from "../services/example";
export default {
  //命名空间
  namespace: "indexTest",
  state: {
    name: "Msea",
  },
  reducers: {
    setName(state, payLoad) {
      //react中要改地址才能检测到
      let _state = JSON.parse(JSON.stringify(state));
      _state.name = payLoad.data.name;
      return _state;
    },
    setCnodeDataList(state, payLoad) {
      let _state = JSON.parse(JSON.stringify(state));
      _state.cnodeData = payLoad.data;
      return _state;
    },
    testPath(state, payLoad) {
      console.log("用户页");
      return state;
    },
  },
  //基于es6 generator语法
  effects: {
    *setNameAsync({ payload }, { put, call }) {
      yield put({
        type: "setName",
        data: {
          name: "超人强",
        },
      });
    },
    *testCnode({ payLoad }, { put, call }) {
      let rel = yield call(apis.testCnode);
      if (rel.data) {
        yield put({ type: "setCnodeDataList", data: rel.data.data });
      }
      console.log(rel);
    },
  },
  subscriptions: {
    haha({ dispatch, history }) {
      history.listen(({ pathname }) => {
        if (pathname === "/user") {
          dispatch({
            type: "testPath",
          });
        }
      });
    },
  },
};

routes/IndexPage.jsx

import React from "react";
import { connect } from "dva";
import * as apis from "../services/example";
class IndexPage extends React.Component {
  handleSetName = () => {
    this.props.dispatch({
      type: "indexTest/setName",
      data: {
        name: "猪猪侠",
      },
    });
  };
  handleSetNameAsync = () => {
    this.props.dispatch({
      type: "indexTest/setNameAsync",
      data: {
        name: "猪猪侠",
      },
    });
  };
  testCnode = () => {
    this.props.dispatch({
      type: "indexTest/testCnode",
    });
  };
  componentDidMount() {
    //   apis.testCnode().then((res) => {
    //     console.log(res);
    //   });
    apis.mockdata().then((res) => {
      console.log(res);
    });
  }

  render() {
    return (
      <div>
        我是首页
        {this.props.msg}
        <div>{this.props.name}</div>
        <button onClick={this.handleSetName}>setName</button>
        <button onClick={this.handleSetNameAsync}>setNameAsync</button>
        <button onClick={this.testCnode}>testCnode</button>
      </div>
    );
  }
}

const mapStateToProps = (state) => {
  return {
    msg: "我爱北京天安门",
    name: state.indexTest.name,
    cnodeData: state.indexTest.cnodeData,
  };
};
export default connect(mapStateToProps)(IndexPage);

routes/userPage.jsx

import React, { Component, Fragment } from "react";
import { Link } from "react-router-dom";
import Child from "../components/child";
class userPage extends Component {
  handleToIndex = () => {
    this.props.history.push("/");
  };
  render() {
    return (
      <Fragment>
        <div>我是用户页</div>
        <Link to="/">首页</Link>
        <button onClick={this.handleToIndex}>首页</button>
        <Child />
      </Fragment>
    );
  }
}

export default userPage;

services/examples.js

import request from "../utils/request";
const pox = "/apis/";
export function query() {
  return request("/api/users");
}

export function testCnode() {
  return request(pox + "/api/v1/topics");
}

//注册mock接口
export function mockdata() {
  return request("/api/mockdta");
}

index.js

import dva from "dva";
import "./index.css";

// 1. Initialize
const app = dva();

// 2. Plugins
// app.use({});

// 3. Model
app.model(require("./models/indexTest").default);

// 4. Router
app.router(require("./router").default);

// 5. Start
app.start("#root");

router.js

import React from "react";
import { Router, Route, Switch } from "dva/router";
import IndexPage from "./routes/IndexPage";
import userPage from "./routes/userPage";
function RouterConfig({ history }) {
  return (
    <Router history={history}>
      <Switch>
        <Route path="/" exact component={IndexPage} />
        <Route path="/user" exact component={userPage} />
      </Switch>
    </Router>
  );
}

export default RouterConfig;

.roadhogrc.mock.js

export default {
  ...require("./mock/testMock"),
};

.webpackrc

{
  "proxy":{
    "/apis":{
      "target":"https://cnodejs.org",
      "changeOrigin":true,
      "pathRewrite":{"^/apis":""}
    }
  }
}

猜你喜欢

转载自blog.csdn.net/nxcniuxiangchen/article/details/121737732
今日推荐