[英]React Hook useMemo has a missing dependency: 'handleClearData'
This is my code below这是我下面的代码
const Search = ({ results }) => {
const dispatch = useDispatch();
const handleClearData = () => {
dispatch(clearData());
};
const columns = useMemo(
() => [
{
Header: 'G35 Number',
accessor: 'g35Number',
Cell: props => {
const { original } = props.row;
return (
<React.Fragment>
<Link data-tip data-for={`link-${original.sNumber}`}
to={{ pathname: `/search/${original.sNumber}`, state: { s: props.row.original } }} onClick={handleClearData}>
{original.g35Number}
</Link>
</React.Fragment>
);
},
},
{
Header: 'Regn Number',
accessor: 'regNumber',
},
{
Header: 'File',
accessor: 'file',
},
{
Header: 'Details',
accessor: 'details',
}
],
[]
);
const data = useMemo(() => results, [results]); // eslint-disable-line react-hooks/exhaustive-deps
const renderTable = () => {
return (
<ReactTable columns={columns} data={data} />
);
}
return (
<div className="card py-3 px-4">
{
results?.length ?
renderTable() :
<div>No results.</div>
}
</div>
);
}export default Search;
I am getting the below warning:我收到以下警告:
React Hook useMemo has a missing dependency: 'handleClearData'. Either include it or remove the dependency array
I tried to add handleClearData in the dependency array of the useMemo, which gave me the below warning:我尝试在 useMemo 的依赖项数组中添加 handleClearData,这给了我以下警告:
The 'handleClearData' function makes the dependencies of useMemo Hook change on every render. Move it inside the useMemo callback. Alternatively, wrap the 'handleClearData' definition into its own useCallback() Hook
I did not understand what it meant when it said that I need to wrap it in its own useCallback() hook.当它说我需要将它包装在它自己的 useCallback() 钩子中时,我不明白这意味着什么。
Can anyone help me what I am missing?任何人都可以帮助我我所缺少的吗? I am not sure if I want to add anything in the dependency array if I just want to load it only for the first time (That if it works the same way in useEffect).
如果我只想第一次加载它,我不确定是否要在依赖项数组中添加任何内容(如果它在 useEffect 中以相同的方式工作)。
I was able to do resolve the warning by moving the function inside useMemo like below:我能够通过移动 useMemo 中的函数来解决警告,如下所示:
{
Header: 'G35 Number',
accessor: 'g35Number',
Cell: props => {
const { original } = props.row;
const dispatch = useDispatch();
const handleClearData = () => {
dispatch(clearData());
};
return (
<React.Fragment>
<Link data-tip data-for={`link-${original.sNumber}`}
to={{ pathname: `/search/${original.sNumber}`, state: { s: props.row.original } }} onClick={handleClearData}>
{original.g35Number}
</Link>
</React.Fragment>
);
},
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.