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