一.三个基础原则
- Single source of truth
撤销/重做会很容易实现(状态存在同一棵树下) - state is read only
view和network callbacks不会直接写入state
按序执行state改变
store.dispaich({
type: ‘COMPLETE_TODO’,
index:1
}) - Changes are made with pure function
pure function:可设置顺序,传输额外数据,reducer可复用(编页码)1
2import {combineReducers,createStore} from 'redux'
//createStore:通过传入reducer形成一个全局唯一的store
store对象有3种方法:store.dispatch(),store.subscribe,store.getState()
connect函数具体工作:把Store上的状态转化为内层傻瓜函数的prop(mapStateToProps);把内层傻瓜组件中的用户动作转化为派送给Store的动作(mapDispatchToProps)
Provider:store必须包含三个函数的object,即subscribe、dispatch、getState