javascript - 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
参数数组(也欢迎对我的方法发表评论 - 它避免使用看起来很重的包来做类似的事情)
解决方案
推荐阅读
- swift - Pod 安装失败
- javascript - 使用数组值进行嵌套过滤
- javascript - 通过 Javascript 将静态 HTML 元素中心保持在屏幕上(在另一个可滚动的 div 下)
- javascript - express API 从 URL 中提取数据
- python-3.x - 计算新数据框中行值的总和
- c++ - C++ - 抽象类的 shared_ptr
- spring-data-elasticsearch - spring-data-elasticsearch 使用额外的多字段修改索引映射
- highest - 选择表中具有最高值出现版本列的所有项目的计数
- c# - Unity:尝试调用事件时出错
- tensorflow2.0 - AttributeError:“AutoTrackable”对象在运行从保存的模型加载的 tf.estimator 模型时没有属性“评估”