简体   繁体   English

React Hook useCallback 有一个不必要的依赖:'GqlUserResponse'

[英]React Hook useCallback has an unnecessary dependency: 'GqlUserResponse'

Hey i'm getting error in this code嘿,我在这段代码中遇到错误

interface GqlUserResponse {
    data: {
      listUsers: {
        items: UserType[]
      }
    },
    errors?: any[]
  }

  useEffect(() => {
    onAuthUIStateChange((
      nextAuthState: any,
      authData: any,
    ) => {
      setAuthState(nextAuthState);
      if (authState !== AuthState.SignedOut) {
        console.log('authData', authData);
        setEmail(authData.attributes.email);
      }
    });
  },
  [authState]);

  const fetchUser = useCallback(async () => {
    try {
      const filter = {
        email: {
          eq: email, // filter priority = 1
        },
      };
      const result = await API.graphql(
        graphqlOperation(listUsers, { filter }),
      ) as GqlUserResponse;
      console.log('result', result?.data?.listUsers.items[0]);
      // setUser(result?.data?.listUsers?.items[0]);
    } catch (res) {
      // eslint-disable-next-line no-console
      console.error('error fetching userFiles', res);
    }
  }, [GqlUserResponse, email]);

  useEffect(() => {
    fetchUser();
  }, [fetchUser]);

error: Line 78:6: React Hook useCallback has an unnecessary dependency: 'GqlUserResponse'.错误:第 78:6 行:React Hook useCallback 有一个不必要的依赖项:'GqlUserResponse'。 Either exclude it or remove the dependency array.排除它或删除依赖数组。 Outer scope values like 'GqlUserResponse' aren't valid dependencies because mutating them doesn't re-render the component react-hooks/exhaustive-deps any toughs像“GqlUserResponse”这样的外部 scope 值不是有效的依赖项,因为改变它们不会重新渲染组件 react-hooks/exhaustive-deps 任何困难

The error message is very clear and helpful.错误消息非常清晰且很有帮助。 Remove GqlUserResponse from the dependency array of useCallback .GqlUserResponse的依赖数组中useCallback

The dependency array is used to define when useCallback (or, similarly, useMemo ) should stop using its memoized value and reinitialize.依赖数组用于定义useCallback (或类似的useMemo )何时停止使用其记忆值并重新初始化。 As the error message states...mutating outer scope values (in this case your interface itself ) isn't going to cause a renrender, so it doesn't belong in the dependency array.正如错误消息所述...改变外部 scope 值(在这种情况下是您的接口本身)不会导致重新渲染,因此它不属于依赖项数组。

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

相关问题 React Hook useCallback 缺少依赖项: - React Hook useCallback has a missing dependency: React Hook useEffect/useCallback 缺少依赖项 - React Hook useEffect/useCallback has a missing dependency React hook 有一个不必要的依赖 - React hook has an unnecessary dependency React Hook useCallback 有一个不必要的依赖:'price'。 排除它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useCallback has an unnecessary dependency: 'price'. Either exclude it or remove the dependency array react-hooks/exhaustive-deps React Hook useMemo有一个不必要的依赖:'匹配' - React Hook useMemo has an unnecessary dependency: 'match' React Hook useCallback 缺少依赖项警告,但存在依赖项 - React Hook useCallback has a missing dependency warning, but the dependencies are present 如何正确修复 React Hook useCallback 缺少依赖项 - How to properly fix React Hook useCallback has a missing dependency Cube.js React Hook React.useCallback 缺少一个依赖项:'pivotConfig'。 + - Cube.js React Hook React.useCallback has a missing dependency: 'pivotConfig'. + React hooks useCallback 依赖于函数 - React hooks useCallback has dependency on function React Hook useCallback 缺少依赖项。 要么包含它,要么移除依赖数组 react-hooks/exhaustive-deps - React Hook useCallback has a missing dependency. Either include it or remove the dependency array react-hooks/exhaustive-deps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM