简体   繁体   English

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

import React, {Fragment, useState,useCallback } from "react";

const ProcessingSearch = () => {
  const [price, setPrice] = useState({ maxPrice: 0, minPrice: 0 });

  const inputMaxMin = useCallback(
    ({ target: { value, name } }) => {
      name === "maxPrice"
        ? setPrice(({ minPrice }) => ({ maxPrice: value, minPrice }))
        : setPrice(({ maxPrice }) => ({ minPrice: value, maxPrice }));
    },
    [price]
  );

  return (
    <Fragment>
      <form onSubmit={() => {}}>
        {"Min"}
        <input
          {...ProcessingSearchInputPrice}
          value={price.minPrice}
          onChange={inputMaxMin}
        />
        {"Max"}
        <input
          {...ProcessingSearchInputPrice}
          value={price.maxPrice}
          onChange={inputMaxMin}
        />
        <input type="submit" title={"Submit price range"} value={"Go"} />
      </form>
    </Fragment>
  );
};

When I use price I get an error:当我使用价格时,我收到一个错误:

React Hook useCallback has an unnecessary dependency: 'price'. React Hook useCallback 有一个不必要的依赖:'price'。 Either exclude it or remove the dependency array react-hooks/exhaustive-deps排除它或删除依赖数组 react-hooks/exhaustive-deps

Its a warning for useCallback implementation (not because of price usage).它是useCallback实现的警告(不是因为price使用)。

As the warning states, remove price variable from the dependencies array [price] :正如警告所述,从依赖项数组[price]删除price变量:

const inputMaxMin = useCallback(
    ({ target: { value, name } }) => {
      name === "maxPrice"
        ? setPrice(({ minPrice }) => ({ maxPrice: value, minPrice }))
        : setPrice(({ maxPrice }) => ({ minPrice: value, maxPrice }));
    },
   []                 // <--- remove price, not used within the hook.
  );

In this case, I believe you can remove the use of useCallback because you don't memoize anything, check the example.在这种情况下,我相信您可以删除useCallback的使用,因为您不会useCallback任何内容,请查看示例。

编辑检查如果渲染

I can't figure out why screens are not needed as dependency and why I have to remove them.我无法弄清楚为什么不需要screens作为依赖项以及为什么我必须删除它们。

编辑 dazzling-kapitsa-d0i9d

暂无
暂无

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

相关问题 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 React Hook useEffect 缺少依赖项要么包含它,要么删除依赖项数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:&#39;user.id&#39;。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'user.id'. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项。 要么包含它,要么移除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:&#39;fetchProfile&#39;。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'fetchProfile'. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:&#39;formValues&#39;。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'formValues'. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:包含它或删除依赖项数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: Either include it or remove the dependency array react-hooks/exhaustive-deps 第 93:6 行:React Hook useEffect 缺少依赖项:'estado'。 要么包含它,要么移除依赖数组 react-hooks/exhaustive-deps - Line 93:6: React Hook useEffect has a missing dependency: 'estado'. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:'loading'。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'loading'. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:&#39;dispatch&#39;。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'dispatch'. Either include it or remove the dependency array react-hooks/exhaustive-deps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM