---redux---
reudx是flux的进阶版,解决了dispatcher和store之间管理的麻烦之处;使用了reducers
1、redux的数据流程
1、如果组件想要修改store里的数据的时候,需要创建一个action,通过store.dispatch(action)将action发送给store
handleAdd(){
const action ={
type:"",
value:
}
store.dispatch(action)
}
2、store会将这个action发送给reducers,reducers会对action之中的type进行比较,然后做一个相应的数据处理,返回给store一个新的state
reducers:
(1)需要有一个默认的state
(2)必须返回一个纯函数,里面的state是只读的,不允许修改
const defaultState={}
export default ((statte=defaultState,action))=>{
return state
}
3、store将新的state返回给组件
创建store(store是redux提供的)
import {createStore} from "redux"
再引入对应的reducers
const store =createStore(reducers)
4、在组件内通过store.subscribe(this.handleUpdate.bind(this))来监听数据的改变
2、合并reducers
使用combineReducers
import {combinereducers} from "redux"
const reducers=combineReducers({对应的educers})