状态管理之redux概念综述(未完待更)

2018-12-28  本文已影响0人  leonC走来

Redux

store

存储state,唯一store。
创建store
createStore([reducer], [preloadedState], applyMiddleware([middlewares]))

Provider

reducer

唯一可直接修改state的方式,为function,传入state,action,返回state一个新的对象

combineReducers

组合多个reducer

官方示例

rootReducer = combineReducers({potato: potatoReducer, tomato: >tomatoReducer})
// This would produce the following state object
{
 potato: {
   // ... potatoes, and other state managed by the potatoReducer ...
 },
 tomato: {
   // ... tomatoes, and other state managed by the tomatoReducer, maybe >some nice sauce? ...
 }
}

action

带有key为type的一个object,引入redux-thunk时可为function以处理异步逻辑。
通过dispatch([action])的方式,发起一个动作 => 中间件处理逻辑之后 => 调起reducer执行对应type的代码,返回新的state。

dispatch

发起一个动作,传入action,该方法由store提供。
store.dispatch

上一篇下一篇

猜你喜欢

热点阅读