首页 > 解决方案 > 反应更新后 useReducer 钩子需要 3 个参数

问题描述

我在更新 react 和 @types/react 版本时遇到问题16.9.0(如果我升级更多,这个问题仍然存在)。它说:Expected 3 arguments, but got 2.

代码:

const [state, dispatch] = React.useReducer<State<D>, Action<D>>(reducer, {
    loading: false,
    data: undefined,
    error: undefined
  });

我试图找到第三个参数是什么,但我发现它只是可选的。也许有人有同样的问题?

标签: reactjstypescriptreact-hooks

解决方案


这应该在没有State<D>, Action<D>类型变量的情况下定义:

const [state, dispatch] = React.useReducer(reducer, {
    loading: false,
    data: undefined,
    error: undefined
});

推荐阅读