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

155
Visualizações
How to know if its the props that changed or the inner state in useEffect

I have a component with inner state that I want to update its initial state using props so I have to watch the props too. however I'd like to know when its the props that changed so that I run the function related to it once and keep it away from the inner component state.\

This is because I am unable to properly update the inner state as that thing will always run everytime the useEffect triggers

the following useEffect is for a Modal that I am setting visible or not from the parent component state(so its always mounted).

useEffect(() => {
    // I want to run the following once
    if (Object.getOwnPropertyNames(propState).length !== 0) {
      console.log(propState);
      setState(()=> {
       innerState = propState.user_name
      });
      
    }
  }, [propState, innerState]);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can check value in inner state and propState. If they equal, when skip setState.

useEffect(() => {
    if (propState?.user_name && propState !== innerState.user_name) {
      setState(()=> {
       innerState = propState.user_name
      });
      
    }
  }, [propState, innerState]);
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