简体   繁体   English

React 自定义钩子没有获取新值

[英]React custom hook not picking up new value

I'm still trying to wrap my head around how React Hooks work.我仍在努力思考 React Hooks 的工作原理。 In Building this custom Hook, I noticed a peculiar behavior:在构建这个自定义 Hook 时,我注意到一个奇怪的行为:

const useCustomHook = () => {
  const [value, setValue] = useState('hello');

  useEffect(() => {
    window.addEventListener('scroll', print, false);
    return () => window.removeEventListener('scroll', print, false);
  }, []);

  const print = () => console.log('print value: ', value); // shows updated value when called directly but original value when scrolling

  return [setValue, print];
};

The peculiar behavior is what print outputs to the console.特殊的行为是print输出到控制台的内容。 When calling print directly, the value reflects the correct updated value.直接调用print ,该值反映了正确的更新值。 But when scrolling, print always outputs the original value to the console.但是在滚动时, print总是将原始值输出到控制台。

Example usage:用法示例:

App.js应用程序.js

let counter = 0;

const App = () => {
  const [setValue, print] = useCustomHook();

  return (
    <div style={{ margin: '50px auto 10000px' }}>
      <button onClick={() => setValue(counter++)}>Increment</button>{' '}
      <button onClick={print}>Print</button>
    </div>
  );
};

Steps:脚步:

  1. Click Increment单击增量
  2. Click Print -> console: print value: 0单击打印-> 控制台: print value: 0
  3. Scroll -> console: print value: hello滚动 -> 控制台: print value: hello

Notice is step 3, the value is still hello .注意是第 3 步,值仍然是hello How can I get the proper value to be printed when scrolling?滚动时如何获得要打印的正确值?

You need to remove the dependencies from useEffect .您需要从useEffect删除依赖useEffect

useEffect(() => {
    window.addEventListener('scroll', print, false);
    return () => window.removeEventListener('scroll', print, false);
  }); // remove the empty array

If you set it to [] , then it will be executed only once.如果将其设置为[] ,则它只会执行一次。
If you remove it, it will be executed on every update.如果删除它,它将在每次更新时执行。

Ideally, you want to re-evaluate the code in useEffect only when value changes.理想情况下,您只想在value更改时重新评估useEffect的代码。 You could then add print to the array of dependencies.然后,您可以将print添加到依赖项数组。

const useCustomHook = () => {
  const [value, setValue] = useState(-1);

  // useCallback will re-create the function only if 'value' changes
  const print = useCallback(() => {
      console.log('print value: ', value);
  }, [value]);

  // useEffect will be re-evaluated if 'print' changes
  useEffect(() => {
    window.addEventListener('scroll', print, false);
    return () => window.removeEventListener('scroll', print, false);
  }, [print]);

  return [setValue, print];
};

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

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