
























首先,useReducer 和使用 redux 十分类似。但是useReducer不是一个整合的store,redux是。
userReducer中的dispatch是各自独立的,不像redux,是共同的。
如果你的state被多个component引用,请使用useReducer。
1 let memoizedState; 2 function useReducer(reducer, initialArg, init) { 3 let initState = void 0; 4 if (typeof init !== 'undefined') { 5 initState = init(initialArg) 6 } else { 7 initState = initialArg 8 } 9 function dispatch(action) { 10 memoizedState = reducer(memoizedState, action) 11 render() 12 } 13 memoizedState = memoizedState || initState 14 return [memoizedState, dispatch] 15 } 16 17 function useState(initState) { 18 return useReducer((oldState, newState) => newState, initState) 19 }
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。