简体   繁体   English

反应 useMemo 缺少依赖项

[英]React useMemo missing dependency

  const handleTestDelete = (id: any) => {
    deleteTest(id).then(() => {
      queryClient.invalidateQueries("test");
    });
  };

  const columns = useMemo(
    () => [
      { field: "id", headerName: "ID", width: 80 },
      {
        field: "",
        width: 120,
        disableClickEventBubbling: true,
        sortable: false,
        disableColumnMenu: true,
        renderCell: (cell: any) => (
          <>
            <IconButton
              aria-label="delete"
              onClick={() => handleTestDelete(cell.id)}
            >
              <DeleteIcon fontSize="small" />
            </IconButton>
            <IconButton
              aria-label="view"
              onClick={() => history.push(`/something/${cell.id}/details`)}
            >
              <VisibilityIcon fontSize="small" />
            </IconButton>
          </>
        ),
      },
    ],
    [history]

I am getting this warning我收到此警告

React Hook useMemo has a missing dependency: 'handleTestDelete'. React Hook useMemo 缺少一个依赖项:'handleTestDelete'。 Either include it or remove the dependency array.包括它或删除依赖数组。

because I am not adding the function called on click of the delete button in dependencies... why should I put it as a dependency?因为我没有添加在依赖项中单击删除按钮时调用的 function ... 为什么我要把它作为依赖项? I'm not even sure if putting history as a dependency is correct;我什至不确定将历史作为依赖项是否正确; I don't think columns need to be reevaluated when chronology changes我认为当年表发生变化时不需要重新评估专栏

I was wrong?我错了?

Thank you谢谢

If you are using useMemo , you should satisfy its dependencies as the suggested by eslint-plugin-react-hooks :如果您使用的是useMemo ,则应按照eslint-plugin-react-hooks的建议满足其依赖关系:

const history = useHistory()

const columns = useMemo(() => {
  console.log("define memoized columns") // Log 1
  return [
    { field: 'id', headerName: 'ID', width: 80 },
    {
      // ... code here
      renderCell: (cell: any) => (
        <>
          <IconButton
            aria-label="delete"
            onClick={() => handleTestDelete(cell.id)}
          >
            <DeleteIcon fontSize="small" />
          </IconButton>
          <IconButton
            aria-label="view"
            onClick={() => history.push(`/something/${cell.id}/details`)}
          >
            <VisibilityIcon fontSize="small" />
          </IconButton>
        </>
      ),
    },
  ]
}, [history, handleTestDelete])

Using history would be safe because it won't change (unless it is changed in React Router's context which is unlikely in this example).使用history是安全的,因为它不会改变(除非它在 React Router 的上下文中被改变,这在本例中不太可能发生)。 And, you would need to wrap that function in a useCallback :而且,您需要将 function 包装在useCallback中:

const handleTestDelete = useCallback((id: any) => {
  deleteTest(id).then(() => {
    queryClient.invalidateQueries('test')
  })
}, []) // <---- Here, you need to add the dependencies as well (maybe queryClient)

You may have one more option here: Remove handleTestDelete from the useMemo 's dependencies and define the handleTestDelete function inside the useMemo ;您可能还有一个选择:从handleTestDelete的依赖项中删除useMemo并在 useMemo 中定义handleTestDelete useMemo before returning the memoized columns.在返回记忆的列之前。

Note: As I don't see your complete code, I would suggest that you test this code and make sure that the Log 1 in useMemo is printed appropriately (only once).注意:由于我没有看到您的完整代码,我建议您测试此代码并确保正确打印useMemo中的Log 1 (仅一次)。


Similar posts:类似帖子:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM