结合老师开始讲的几个Hook特性,我觉得可以这样构建Redux
1. 创建一个Store:Store = createContext()
2. 编写 reduer: reducer() { switch(type) { add .. remove... } }
3. 使用useReducer存储state和dispatch: [state, dispathc] = useReducer(reducer, initialState)
4. 将[state, dispathc] 赋值给Provider的vaule export出去: export:
const value = {state, dispatch} return <Store.Provider value={value} />
5. export的Provider组件挂载到root <app>
6. 在页面中import Store, 通过 useContext(Store) 获取到{ state, dispatch}
const { state, dispatch} = useContext(Store)
action调用:dispatch({ type: 'add', padload:{ } })
state获取: state.todos