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]);