react-hooks - react-hooks/exhaustive-deps 用于传递到/从自定义钩子的参考
问题描述
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 被视为 prop,因此警告是从 eslint 报告的。与从 导出的 ref 相同useAnotherHook
。
如何配置 eslint 或更改代码?
解决方案
更改useEffect
为useCustomHook
:
const useCustomHook = (refs) => {
const { someRef } = refs;
React.useEffect(() => {
console.log(someRef.current);
}, [someRef]);
};
并更改useEffect
为Component
:
React.useEffect(() => {
console.log(anotherRef.current);
}, [anotherRef]);
推荐阅读
- cognos - cognos 中的 Gateway URI 和 Dispatcher URI 有什么区别
- macos - 在 macOS 上本地使用 TCP 套接字时出现异常超时
- python - 如何在 Tensorflow 中扩充文本数据集?
- reactjs - 如果路径包含“?”,如何添加路由?
- java - 跨多个实体使用相同 UUID 的策略
- google-api - Google Rest API 和 google 登录
- python - Pyttsx3 不读取其他语言的文本
- ios - AVPlayer 无法播放 AVURLAsset 加载的 mp3 格式文件,其中选项 AVURLAssetPreferPreciseDurationAndTimingKey 在 iOS 14 上设置为 true
- reactjs - 如何将我的过滤器选项重置为其初始空白状态?反应
- google-cloud-pubsub - 如何将我的 Google 商家评论更新发布到我的主题