【问题标题】:React: how to initialize reducerReact:如何初始化reducer
【发布时间】:2021-10-05 13:04:49
【问题描述】:

您好,我正在尝试学习 useReducer 以便在将来使用它,但我遇到了一个错误。 当我尝试初始化它时,我无法传递初始状态

这是我的代码:

const initialState = {
  count: 0,
};

const reducer = () => {
  console.log('hello');
};

const Test = (): ReactElement => {
  const [state, dispatch] = useReducer(reducer, initialState);

  return <div>test</div>;
};

我在 initialState 上收到错误提示:

Argument of type '{ count: number; }' is not assignable to parameter of type 'void'

我确切地说我使用打字稿

【问题讨论】:

    标签: reactjs typescript react-hooks


    【解决方案1】:

    我发现了错误。这是因为我的 reducer 函数没有返回任何内容

    【讨论】:

      猜你喜欢
      • 2017-09-04
      • 1970-01-01
      • 2019-05-28
      • 2017-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      相关资源
      最近更新 更多