【问题标题】:Line 93:6: React Hook useEffect has a missing dependency: 'estado'. Either include it or remove the dependency array react-hooks/exhaustive-deps第 93:6 行:React Hook useEffect 缺少依赖项:'estado'。要么包含它,要么移除依赖数组 react-hooks/exhaustive-deps
【发布时间】:2021-12-28 06:32:07
【问题描述】:

有人可以帮帮我吗?

我有以下问题!!

第 93:6 行:React Hook useEffect 缺少依赖项:'estado'。要么包含它,要么移除依赖数组 react-hooks/exhaustive-deps

  const [nome, setNome] = useState("");
  const [sobreNome, setSobreNome] = useState("");
  const [email, setEmail] = useState("");
  const [dataNascimento, setDataNascimento] = useState("");
  const [phone, setPhone] = useState("");
  const [estado, setEstado] = useState();
  const [cidade, setCidade] = useState();   
  const [lojas, setLojas] = useState();
  const [valueEstado, setValueEstado] = useState([]);
  const [valueUnidade, setValueUnidade] = useState([]);
  const navigate = useNavigate();

  useEffect(() => {
    let arr = [];
    unidades.map((item) => {
      if (estado === item.UF) {
        arr.push(item.Cidade);
      }
      return true
    });
    
    setValueEstado(arr);
  }, [estado]);

  useEffect(() => {
    let arr = [];
    unidades.map((item) => {
      if (cidade === item.Cidade && estado === item.UF) {
        arr.push({ Loja: item.Loja, CodigoFilial: item.CodigoFilial });
      }
      return true
    });
    setValueUnidade(arr);
  }, [cidade]);

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    您的第二个 useEffect 访问状态变量“estado”,但您没有将它列在依赖数组中(useEffect 的第二个参数)。请注意,这将导致您的第二个 useEffect 在任何“estado”更改时重新运行。

    【讨论】:

    • 你能以正确的方式重写代码吗?
    • @LucasSilva 你的第二个 useEffect 应该是这样的useEffect(() => {...}, [cidade, estado])
    • 你是最棒的,伙计
    • 解决了我的问题,非常感谢
    猜你喜欢
    • 2020-06-09
    • 2021-10-31
    • 2020-06-18
    • 2021-10-21
    • 2021-07-15
    • 2021-12-06
    • 1970-01-01
    • 2023-01-15
    • 2022-01-09
    相关资源
    最近更新 更多