action
發(fā)起的行為動(dòng)作
reducer
處理器
reducer=(state,action)
//在處理器中放置上次保存的狀態(tài)和即將要做的行為
state
最后的狀態(tài)
如果用react發(fā)送ajax請(qǐng)求
action=>發(fā)起Ajax請(qǐng)求
reducer=>處理json數(shù)據(jù)
state=>渲染到UI
源碼解讀
const initlState={};
const action={
type:'init',
payload:'hello world',
};
//action必須為對(duì)象
const reducer=(state,action)=>{
return Object.assign({}, action);//return action;
}
//createStore()返回以函數(shù)為屬性的對(duì)象
const store=createStore(reducer,initlState);
//store.dispatch(action)會(huì)調(diào)用reducer(store.getState(),action),結(jié)果返回給currentState這個(gè)閉包量
store.dispatch(action);
//store.getState()返回閉包量currentState
console.log(store.getState());