小编mas*_*hef的帖子

如何配置 eslint 规则以在全局范围内忽略 react-hooks/exhaustive-deps?

我有一个带有 useEffect 钩子的 react 组件,如下所示:

const LocationEditor: React.FC<SectionEditorProps> = (props) => {
const [section, setSection] = useState({...(props.section as Location)});
useEffect(() => {
    props.onChange(section);
}, [section]);
Run Code Online (Sandbox Code Playgroud)

我在运行项目时收到此警告:

React Hook useEffect has a missing dependency: 'props'. Either include it or remove the dependency array. However, 'props' will change when *any* prop changes, so the preferred fix is to destructure the 'props' object outside of the useEffect call and refer to those specific props inside useEffect  react-hooks/exhaustive-deps
Run Code Online (Sandbox Code Playgroud)

所以我尝试将组件更改为如下所示,解构道具:

const LocationEditor: React.FC<SectionEditorProps> = ({section, …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs eslint eslintrc typescript-eslint

4
推荐指数
1
解决办法
3876
查看次数