一、使用redux的流程
1.定义动作类型
const INCREMENT = 'INCREMNET';
2.定义项目的默认状态,传入reducer
let initState = {...};
function reducer(state = initState, action) {
//...
}
3.编写reducer,实现更新state的具体逻辑
function reducer(state=initState,action){
let newState;
switch(action.type){
//...
}
return newState;
}
4.创建容器,传入reducer
let store=createStore(reducer);
5.订阅需要的方法,当state改变会自动更新
store.subcribe(function(){});
6.在需要更新state的地方调用dispatch即可
store.dispatch(/*某个action*/);
可以看到通过以上几个步骤,就可以使用redux,且不局限于某种“框架”中,redux是一个设计思想,只要符合你的需求就可以使用redux。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。