简体   繁体   English

typescript es-lint 错误:react-hooks/exhaustive-deps

[英]typescript es-lint error: react-hooks/exhaustive-deps

I first thought it's a false positive, but until now I have no clue why the error is a helpful thing.我最初认为这是一个误报,但直到现在我不知道为什么这个错误是有用的。

How to make sense react-hooks/exhaustive-deps error when your external props function is used in useEffect doesn't need to be put in dependency?当您的外部道具 function 用于useEffect时,如何理解react-hooks/exhaustive-deps错误不需要依赖?

interface props {
  someExternalPropFunction: any;
}

const App: React.FC<props> = ({ someExternalPropFunction }) => {
  const [formValues, setFormValues] = React.useState<initialStateProps>({
    eventInfo: {
      name: "",
      location: ""
    }
  });

  React.useEffect(() => {
    someExternalPropFunction(formValues);
  }, [formValues]); //what is going on here?

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
    </div>
  );
};

https://codesandbox.io/s/tender-swirles-cu0qw https://codesandbox.io/s/tender-swirles-cu0qw

The dep list needs someExternalPropFunction since the prop might change. dep 列表需要someExternalPropFunction因为道具可能会改变。

  React.useEffect(() => {
    someExternalPropFunction(formValues);
  }, [someExternalPropFunction, formValues]); // now it is fixed

codesandbox, fixed密码箱,固定

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

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