useReducer可以与数组作为状态一起使用吗?
- 论坛
- useReducer可以与数组作为状态一起使用吗?
9 浏览
useReducer可以与数组作为状态一起使用吗?
我已经实现了一个相当简单的方法来为useReducer
添加撤销功能,具体如下:
export const stateMiddleware = reducerFunc => { return function(state = { history: [] }, action) { return { history: action.type === UNDO ? drop(state.history) // lodash drop : [reducerFunc(state.history[0], action), ...state.history], } } }
和
const [state, dispatch] = useReducer(stateMiddleware(reducer), { history: [initialState] })
state
变量包含了整个历史记录,所以需要从第一个元素中提取出当前状态。
我想知道useReducer
是否接受一个数组作为state
参数(同时也欢迎对我的方法提出评论 - 它避免了使用似乎庞大的软件包来完成类似的事情)。