简体   繁体   English

React - 尽管程序运行良好,但 useMemo 发出警告。 (useMemo 有一个不必要的依赖)

[英]React - useMemo giving warning although program is running fine. (useMemo has an unnecessary dependency)

First of all sorry for bad code.首先对糟糕的代码感到抱歉。 I am new to React, so please excuse me:P.我是 React 新手,所以请原谅:P。 Here I have created 2 buttons, "add" and "Change fake".在这里,我创建了 2 个按钮,“添加”和“更改假”。 When I click on "add" it will not re-render child component and when I click on "change fake", It does re-render the component because my dependency has changed in useMemo and it creates and sends a new object props to child.当我点击“添加”时,它不会重新渲染子组件,当我点击“更改假”时,它会重新渲染组件,因为我的依赖在 useMemo 中发生了变化,它会创建并发送一个新的 object 道具给孩子. But I am getting a warning for useMemo saying it's an unnecessary dependency although I need to dependency.但是我收到 useMemo 的警告,说它是不必要的依赖,尽管我需要依赖。 How can I go that warning go away.我怎么能 go 警告 go 离开。 Is there a better way to do this?有一个更好的方法吗?

Note: My child component is a dummy component.注意:我的子组件是一个虚拟组件。 It does not do anything with those props.它对这些道具没有任何作用。

import Child from "./Child";

const Parent = () => {
  const [count, setCount] = useState(0);
  const fakeValue = useRef(10);

  const randomProp = useMemo(() => {
    return { val: fakeValue.current };
  }, [fakeValue.current]);

  const changeFake = () => {
    fakeValue.current = fakeValue.current + 1;
    setCount(count + 1);
  };

  return (
    <>
      <div>{count}</div>
      <div>{fakeValue.current}</div>
      <button onClick={() => setCount(count + 1)}>add</button>
      <button onClick={() => changeFake()}>Change fake</button>
      <Child randomProp={randomProp} />
    </>
  );
};

export default Parent; 

Output link: https://i.stack.imgur.com/5spw1.png CodeSandbox link: https://codesandbox.io/s/silly-pine-fzjw9?file=/src/ Output 链接: https://i.stack.imgur.com/5spw1.png代码沙盒链接:Z5E056C500A1C4B6A7110B50D807BADE/s-rc: ///-fzio7BADE/spine.

Maybe a dumb question.也许是一个愚蠢的问题。 But I would appreciate some feedback from the community please:)但我会感谢社区的一些反馈:)

fakeValue.current is an unnecessary dependency because mutating it does not re-render the component. fakeValue.current是不必要的依赖,因为改变它不会重新渲染组件。 We do not add references as dependencies.我们不会将引用添加为依赖项。

You can remove the warning by adding an empty array as a dependency to useMemo .您可以通过添加一个空数组作为useMemo的依赖项来删除警告。

const randomProp = useMemo(() => {
  return { val: fakeValue.current };
}, []); // Pass an empty array like this

https://codesandbox.io/s/usememo-warning-67955446-5x8j3 https://codesandbox.io/s/usememo-warning-67955446-5x8j3

Let me know if you need further support.如果您需要进一步的支持,请告诉我。

When you pass fakeValue.current ref directly into dependency of useMemo it will not change value of randomProp .当您将fakeValue.current ref 直接传递给 useMemo 的依赖项时,它不会更改useMemorandomProp then child component wont re-render .然后子组件不会re-render So you need to do something like this.所以你需要做这样的事情。

const randomValue = fakeValue.current;

  const randomProp = useMemo(() => {
    return { val: randomValue };
  }, [randomValue]);

If re-render not required, just want to remove the warning, you can remove fakeValue.current dependency如果不需要重新渲染,只想去掉警告,可以去掉fakeValue.current依赖

CodeSandbox link modified code: https://codesandbox.io/s/stoic-thunder-9ylxi?file=/src/Parent.js CodeSandbox链接修改代码: https://codesandbox.io/s/stoic-thunder-9ylxi?file=/src/Parent.js

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

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