Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

109
Visualizações
UseEffect throwing Warning for useState

Hello Guys I am getting this warning

React Hook useEffect has missing dependencies: 'financialBackground' and 'setFinancialBackground'. Either include them or remove the dependency array. If 'setFinancialBackground' changes too often, find the parent component that defines it and wrap that definition in useCallback react-hooks/exhaustive-deps

This is my Code for useEffect.

useEffect(() => {
    if ((pastRevYear, pastRev)) {
      let objRev;
      objRev = { pastRevYear, pastRev };
      let input = [];
      const check = { ...financialBackground };
      for (let i in check) {
        const item = check[i];
        if (i === "pastRev") {
          for (let j in item) {
            const jitem = item[j];
            if (jitem) {
              input.push(jitem);
            }
          }
        }
      }
      input = [...input, objRev];
      setFinancialBackground({
        ...financialBackground,
        pastRev: input,
      });
    }
  }, [pastRev, pastRevYear]); 

I defined useState like this in parent Component.

 const [financialBackground, setFinancialBackground] = useState({
    fundingMethod: props.data
      ? props.data.financialBackground.fundingMethod
      : "",
    otherFundingMethod: props.data
      ? props.data.financialBackground.otherFundingMethod
      : "",
    pastRev: props.data ? props.data.financialBackground.pastRev : "",
    pastExp: props.data ? props.data.financialBackground.pastExp : "",
  }); 

If I put setFinancialBackground in dependency Array it re renders the state multiple times.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

This is because you are not using the dependencies in useEffect hoook. You are changing financialBackground and hence it should be added to useEffect hook like below

useEffect(() => {
    if ((pastRevYear, pastRev)) {
      let objRev;
      objRev = { pastRevYear, pastRev };
      let input = [];
      const check = { ...financialBackground };
      for (let i in check) {
        const item = check[i];
        if (i === "pastRev") {
          for (let j in item) {
            const jitem = item[j];
            if (jitem) {
              input.push(jitem);
            }
          }
        }
      }
      input = [...input, objRev];
      setFinancialBackground({
        ...financialBackground,
        pastRev: input,
      });
    }
  }, [pastRev, pastRevYear, setFinancialBackground, financialBackground]);
about 4 years ago · Juan Pablo Isaza Relatório

0

It doesn't feel good that the child is updating the parent value this way.

Try instead:

Child

useEffect(() => {
  if ((pastRevYear, pastRev)) {
    // get objRev 
    setRev(objRev);
  }
}, [pastRev, pastRevYear, setRev]);

Parent

const financialBackground = useRef();
const [rev, setRev] = useState();

useEffect(() => {
  // do the rev check here on financialBackground.current then
  financialBackground.current = /* ... */
}, [rev]);

```
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda