开发一个redux应用的步骤就是 1.定义action和与之对应的reducer 2.监听store的变化,提供回调函数 3.dispatch一个action
创建一个状态管理的仓库
const createStore=function(initState,reducer){ let state=initState; let listenerList=[]; let getState=function(){ return state; } let subscribe=function(listener){ listenerList.push(listener); } let dispatch=function(action){ state=reducer(state,action); listenerList.forEach((listener)=>{ listener(); }) } return { subscribe, dispatch, getState } } const initState={ name:'www' } const action={ type:'UPDATE_NAME', value:'xxx' } const reducer=function(state,action){ switch (action.type){ case 'UPDATE_NAME': return { ...state, name:action.value } default:return state } }