简体   繁体   English

React 钩子 useCallback 不反映更新状态

[英]React hooks useCallback not reflecting updated state

I have a simplified version of my problem as follows:我有我的问题的简化版本如下:

const Component = () => {
  const [data, setData] = useState([]);

  const fn = useCallback((num) => {
    const newData = [...data];
    newData.push(num);

    setData(newData);
  }, [data]);

  return <button onClick={() => fn(Math.random())}>{data.join()}</button>;
};

My problem is that newData is always [] , instead of reflecting the updated state values.我的问题是newData总是[] ,而不是反映更新的状态值。 Therefore, my button will only show the latest data value instead of an array with increasing values.因此,我的按钮将只显示最新的data值,而不是具有递增值的数组。 Why is this the case as I've included it in the dependency array of the useCallback function?为什么会出现这种情况,因为我已将其包含在useCallback函数的依赖项数组中?

Your code works just as you expect:您的代码按您的预期工作:

https://codesandbox.io/s/relaxed-ishizaka-n2eg6?file=/src/App.js https://codesandbox.io/s/relaxed-ishizaka-n2eg6?file=/src/App.js

It does show the updated state values.它确实显示了更新的状态值。

I guess the problem is most probably happen in the "button" component which you have simplified in your question therefore we cannot see the real problem.我想问题很可能发生在您在问题中简化的“按钮”组件中,因此我们看不到真正的问题。

There could be multiple reasons why states in the "useCallback" is not updated: “useCallback”中的状态未更新可能有多种原因:

  1. you didn't add the state in the dependency of "useCallback".您没有在“useCallback”的依赖项中添加状态。 This is not your case as you already add data in the dependency这不是您的情况,因为您已经在依赖项中添加了数据

  2. you use this "fn" in another "useCallback" and you didn't add fn as a dependency of that "useCallback".您在另一个“useCallback”中使用了这个“fn”,并且没有添加 fn 作为该“useCallback”的依赖项。

     // what you probably write in the "button" component const handleClick = useCallback(() => { ... doSomethingWithState(someState); props.onClick(); //<--- this is the fn you passed }, [someState]); // what you should write const handleClick = useCallback(() => { ... doSomethingWithState(someState); props.onClick(); //<--- this is the fn you passed }, [someState, props.onClick]); //<---- you need to add the method as dependency as well

please verify if this is your case.请验证这是否是您的情况。

I have a simplified version of my problem as follows:我对我的问题有一个简化的版本,如下所示:

const Component = () => {
  const [data, setData] = useState([]);

  const fn = useCallback((num) => {
    const newData = [...data];
    newData.push(num);

    setData(newData);
  }, [data]);

  return <button onClick={() => fn(Math.random())}>{data.join()}</button>;
};

My problem is that newData is always [] , instead of reflecting the updated state values.我的问题是newData始终为[] ,而不是反映更新后的状态值。 Therefore, my button will only show the latest data value instead of an array with increasing values.因此,我的按钮将仅显示最新的data值,而不显示具有递增值的数组。 Why is this the case as I've included it in the dependency array of the useCallback function?为什么会出现这种情况,因为我已将其包含在useCallback函数的依赖项数组中?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM