react hook/exhaust deps用于refs传递到/从自定义hook


const useCustomHook = (refs) => {
const { someRef } = refs;
useEffect(() => {
console.log(someRef.current);
}, []); // <= react-hooks/exhaustive-deps for someRef
};
const useAnotherHook = () => {
const anotherRef = useRef(null);
return { anotherRef };
};
const Component = () => {
const someRef = useRef(null);
useCustomHook({ someRef });
const { anotherRef } = useAnotherHook();
useEffect(() => {
console.log(anotherRef.current);
}, []); // <= react-hooks/exhaustive-deps for anotherRef
return (<div>Loading</div>);
};

当我在钩子内将ref传递给useCustomHook时,ref被视为道具,因此从esint报告警告。与从useAnotherHook导出的ref相同。

如何配置esint或更改代码?

useCustomHook中的useEffect更改为:

const useCustomHook = (refs) => {
const { someRef } = refs;
React.useEffect(() => {
console.log(someRef.current);
}, [someRef]);
};

并将Component中的useEffect更改为:

React.useEffect(() => {
console.log(anotherRef.current);
}, [anotherRef]);

相关内容

  • 没有找到相关文章

最新更新