useReducer可以与数组作为状态一起使用吗?

9 浏览
0 Comments

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参数(同时也欢迎对我的方法提出评论 - 它避免了使用似乎庞大的软件包来完成类似的事情)。

0