简体   繁体   English

React Hook useMemo有一个不必要的依赖:'匹配'

[英]React Hook useMemo has an unnecessary dependency: 'match'

this my code 这是我的代码

const Head: FC<{ match: faceMatch<{}> }> = ({ match }) => {
  const [arrProd, setArrProd] = useState<JSX.Element[]>([]);
  const [ImgCatIndx, setImgCatIndx] = useState<number>(0);
  const [resError, setResError] = useState<string>("");
  useMemo(() => {
    setImgCatIndx(0);
  }, [match]);//warning React Hook useMemo has an unnecessary dependency 
  useEffect(() => {
    const abortController = new AbortController();
    const signal = abortController.signal;
    document.title = `Hat Jacket Pants Shoes Suit | Amasia`;
    (async () => {
      setArrProd([]);
      setResError("");
      try {
        const Res = await fetch(
          `https://foo0022.firebaseio.com/New/${headcateg[ImgCatIndx]}.json`,
          {
            signal: signal
          }
        );
        if (!Res.ok) {
          throw new Error("Page Not Found 404");
        }
        const ResObj = await Res.json();
        const ResArr = await Object.values(ResObj).flat();
        await setArrProd(
          ResArr.map(
            ({
              to,
              id,
              price,
              src,
              title,
              sold,
              shipping
            }: faceProductList) => (
              <Fragment key={id}>
                <NavLink to={to}>
                  <img
                    src={`/${src[0]}`}
                    alt={title}
                    height={"220px"}
                    width={"220px"}
                  />
                  <span>{price}</span>
                  <span>{shipping}</span>
                  <span>{sold}</span>
                </NavLink>
              </Fragment>
            )
          )
        );
      } catch (error) {
        if (error.name !== "AbortError") {
          setResError(error.message);
        }
      }
    })();
    return () => {
      abortController.abort();
    };
  }, [ImgCatIndx]);

  if (resError !== "") {
    return <HandlerErr error={resError} />;
  } else if (!arrProd.length) {
    return <Loding />;
  }
return(....)

I get in the console. 我进入控制台。

React Hook useMemo has an unnecessary dependency: 'match'. React Hook useMemo有一个不必要的依赖:'匹配'。 Either exclude it >or remove the dependency array. 排除它>或删除依赖关系数组。

I want When changing the match has changed and ImgCatIndx = 0.it's not clear why I'm getting a warning here..How can this be related..How to fix the warning? 我想要当改变比赛已经改变并且ImgCatIndx = 0.时不清楚为什么我在这里收到警告......如何相关......如何解决警告?

您应该使用useEffect而不是useMemo。如果您使用react路由器,则匹配对象会在更新历史记录时更改,即使传递相同的参数并正确传输参数本身也是如此。

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

相关问题 React hook 有一个不必要的依赖 - React hook has an unnecessary dependency React Hook useMemo 缺少依赖项:&#39;handleClearData&#39; - React Hook useMemo has a missing dependency: 'handleClearData' React - 尽管程序运行良好,但 useMemo 发出警告。 (useMemo 有一个不必要的依赖) - React - useMemo giving warning although program is running fine. (useMemo has an unnecessary dependency) React Hook useCallback 有一个不必要的依赖:'GqlUserResponse' - React Hook useCallback has an unnecessary dependency: 'GqlUserResponse' React Hook useMemo 缺少依赖项 - React Hook useMemo has missing dependencies React useMemo hook memoize 是否应该使用相同的依赖值重复调用? - Should the React useMemo hook memoize repeat calls with same dependency values? React Hook useEffect 缺少依赖项:'match.params.id' - React Hook useEffect has a missing dependency: 'match.params.id' React Hook useCallback 有一个不必要的依赖:&#39;price&#39;。 排除它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useCallback has an unnecessary dependency: 'price'. Either exclude it or remove the dependency array react-hooks/exhaustive-deps 反应 useMemo 缺少依赖项 - React useMemo missing dependency 如何在不使用 useMemo 钩子的情况下减少不必要的重新渲染? - How can I reduce unnecessary re-renders in react without using the useMemo hook?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM