-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
经典文章阅读笔记 #12
Comments
217-9-12晨读未读文章在使用react时往往会遇到一些问题
通过immutable来解决以上问题使用 immutable 的边界性问题既然这么好,Immutable.js 如何同现有的 React + Redux 技术方案进行集成呢?好在有redux-immutable(事实上还有一个叫做redux-immutablejs的库也能实现二者的集成)这么一个库,在它的帮助下,可以实现 Immutable.js 的植入。在介绍集成方案之前,我们有必要先划分数据使用的边界,就是在哪些地方使用 Javascript 原生数据结构(简称为JSD),哪些地方使用 immutable,哪些地方需要做二者的转化,如下图所示: 在 React 视图里,props 其实就来自于 Redux 维护的全局的 state 的,所以 props 中的每一项一定是 immutable 的。 为什么要做这种统一呢?是因为: 避免 JSD 和 immutable 的混用导致出错; export default function(state, action) { 如何集成 immutable 到流程中 按照 Redux 的工作流,我们从创建 store 开始。Redux 的 createStore 可以传递多个参数,前两个是: reducers 和 initialState。 reducers 我们用 redux-immutable 提供的 combineReducers 来处理,他可以将 immutable 类型的全局 state 进行分而治之: const rootReducer = combineReducers({ const initialState = Immutable.Map(); 接下来,你会发现,react-router-redux的接入也要改造,因为 routerReducer 是不兼容 immutable 的,所以你必须自定义 reducer: import Immutable from 'immutable'; const initialState = Immutable.fromJS({ export default (state = initialState, action) => { import { const history = syncHistoryWithStore(browserHistory, store, { @connect(state => state.toJS()) @connect(state => state.toObject()) 细心的人可能会发现,在使用 immutable 维护全局的 state 的情况下,组件 props 的校验也需要与时俱进,使用 immutable 类型校验,这就需要我们 import 专门针对 immutable 类型进行校验的库:react-immutable-proptypes,使用方法基本上和普通的 PropTypes 一致: propTypes: { fromJS({ 使用他提供的 decorator 来减少 render 的触发。 |
2017-9-13晨读不错的文章阅读总结Redux进阶系列3:如何设计action、reducer、selector
|
2017-9-21 晨读
|
2017-926 晨读
|
2017-9-6 晨读
The text was updated successfully, but these errors were encountered: