简体   繁体   English

反应 useEffect 钩子和 eslint 警告

[英]React useEffect hook and eslint warning

I'm looking for recommendations about how to solve the following problem:我正在寻找有关如何解决以下问题的建议:

I have a component A that has two states, state1 and state2.我有一个组件 A,它有两个状态,state1 和 state2。

If I want to run an effect only when state1 changes but in the useEFfect callback I use the value of state2, how can I solve the issue about array dependencies eslint react hooks warning?如果我只想在 state1 更改时运行效果,但在 useEFfect 回调中我使用 state2 的值,我该如何解决有关数组依赖 eslint react hooks 警告的问题?

Ex.前任。

function A() {
    const [state1, setState1] = useState(0);
    const [state2, setState2] = useState(0);

    useEffect(() => {
        const a = state1 + state2;
        // do some async logic using a
 
    }, [state1]); //Here eslint react hooks rule requests adding state2 to the dependency array!

return <div> some text </div>

}

The common method would typically be to disable the linting rule specifically for that line.常见的方法通常是禁用专门针对该行的 linting 规则。

function A() {
  const [state1, setState1] = useState(0);
  const [state2, setState2] = useState(0);

  useEffect(() => {
    // do some async logic using a
    const a = state1 + state2;

    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [state1]);

  return <div> some text </div>;
}

But the drawback here is it can mask dependency issues if the dependencies actually change and you forget to update the dependency array.但这里的缺点是,如果依赖项实际发生变化并且您忘记更新依赖项数组,它可能会掩盖依赖项问题。

You can use a React ref and an additional useEffect hook to cache a current state2 value and refer to this in the other useEffect callback.您可以使用 React ref 和附加的useEffect挂钩来缓存当前state2值,并在另一个useEffect回调中引用它。

function A() {
  const [state1, setState1] = useState(0);
  const [state2, setState2] = useState(0);

  const state2Ref = useRef(state2);

  useEffect(() => {
    state2Ref.current = state2;
  }, [state2]);

  useEffect(() => {
    // do some async logic using a
    const a = state1 + state2Ref.current;
  }, [state1]);

  return (
    ...
  );
}

 function A() { const [state1, setState1] = React.useState(0); const [state2, setState2] = React.useState(0); const state2Ref = React.useRef(state2); React.useEffect(() => { state2Ref.current = state2; }, [state2]); React.useEffect(() => { // do some async logic using a const a = state1 + state2Ref.current; console.log("effect called", a); }, [state1]); return ( <div> some text <div> <button type="button" onClick={() => setState1((c) => c + 1)}> Update State 1 </button> <button type="button" onClick={() => setState2((c) => c + 1)}> Update State 2 </button> </div> </div> ); } const rootElement = document.getElementById("root"); ReactDOM.render( <A />, rootElement );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script> <div id="root" />

You need to add // eslint-disable-next-line react-hooks/exhaustive-deps to your userEffect like this:您需要将// eslint-disable-next-line react-hooks/exhaustive-deps添加到您的 userEffect 中,如下所示:

function A() {
    const [state1, setState1] = useState(0);
    const [state2, setState2] = useState(0);

    useEffect(() => {
        const a = state1 + state2;
        // do some async logic using a


       // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [state1]); //Here eslint react hooks rule requests adding state2 to the dependency array!

    return <div> some text </div>

}

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

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