![](/img/trans.png)
[英]React Hook useCallback has an unnecessary dependency: 'GqlUserResponse'
[英]React hook has an unnecessary dependency
你好,我得到了这个:
React Hook useCallback 有一个不必要的依赖:'product.selectedFilesVideos'。 排除它或删除依赖数组 react-hooks/exhaustive-deps
我的状态:
const [product, setProduct] = useState({
.......
selectedFilesVideos: [],
.......
});
我的功能:
const addSelectedFilesVideos = useCallback(
(files) => {
setProduct((prevProduct) => {
return { ...prevProduct, selectedFilesVideos: files };
});
},
[product.selectedFilesVideos]
);
我在哪里使用它:
<FileManger
addSelectedFiles={addSelectedFilesVideos}
selectFiles={product.selectedFilesVideos}
acceptFormat="video/*"
videoOrPics="Drop video files here or click to upload."
/>
你传递给依赖数组product.selectedFilesVideos
,它没有在useCallback
的函数中useCallback
。 此数组用于在更新依赖项之一时调用useCallback
和 update 函数。
这就是为什么会有 React 警告。
更多信息: https : //reactjs.org/docs/hooks-reference.html#usecallback
您可以通过从“useCallback”的依赖项数组中删除product.selectedFilesVideos
来解决警告。 由于回调挂钩在此数组中没有实际依赖项,因此它仍应按预期工作,并且会修复您的警告。
const addSelectedFilesVideos = useCallback(
(files) => {
setProduct((prevProduct) => {
return { ...prevProduct, selectedFilesVideos: files };
});
},
[]
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.