react dva 碎片02
dva 是基于现有应用架构 (redux + react-router + redux-saga 等)的一层轻量封装,没有引入任何新概念,全部代码不到 100 行。( Inspired by elm and choo. ) dva 是 framework,不是 library,类似 emberjs,会很明确地告诉你每个部件应该怎么写,这对于团队而言,会更可控。另外,除了 react 和 react-dom 是 peerDependencies 以外,dva 封装了所有其他依赖。 DVA一共有5个API import dva,{ connect } from 'dva'; // 1. Create app const app = dva(); // 2. Add plugins (optionally) app.use(plugin); // 3. Register models app.model(model); // 4. Connect components and models const App = connect(mapStateToProps)(Component); // 5. Config router with Components app.router(routes); // 6. Start app app.start('#root'); API1. app = dva(opts)新建一个dva app,你可以配置history和initialState选项。
import { browserHistory } from 'dva/router'; const app = dva({ history: browserHistory,}); 2. app.use(hooks)
3. app.model(obj)
put(action)和dispatch(action) yield put({ type: actionType,payload: attachedData,error: errorIfHave}); dispatch({ type: actionType,error: errorIfHave}); call(asyncFunction) const result = yield call(api.fetch,{ page: 1 }); select(function) const count = yield select(state => state.count); 4. app.router(({ history }) => routes)import { Router,Route } from 'dva/routes'; app.router(({ history } => ({ <Router history={ history }> <Route path="/" component={App} /> </Router> }); 5. app.start(selector?)启动应用,selector是可选的。如果没有selector参数,它会返回一个函数,这个函数返回JSX元素。 https://github.com/dvajs/dva (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |