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

89
Visualizações
Run useEffect only after prop is updated

I'm passing a prop necessary to run an API call into my component and I do not want it to run on initial rendering but only once this prop has been updated by a form submission. Even though I'm putting props in the dependency argument it still runs the call on mount. How can I prevent useEffect from automatically running on initial rendering? Maybe there's a completely different method I should be considering?

function GetData(props) {


 const [propsState, propsSetState] = useState('');

 useEffect(() => {

  // Run API Call

 }, [props])
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

const [mounted, setMounted] = useState(false);

useEffect(() => {

  if (!mounted) {
    setMounted(true);
    return;
  }

  // Run API Call

}, [props])
about 4 years ago · Juan Pablo Isaza Relatório

0

const ref = useRef();

useEffect(() => {

  if (!ref.current) {
    ref.current = true
    return;
  }

  // Run API Call

}, [props])
about 4 years ago · Juan Pablo Isaza Relatório

0

In your return statement use logical && to evaluate if something is updated before run the API in your component.

{propState && <*YourComponent* />}

First Fetch the data, once the data is being retrieve, then update the state in your const[], once it has the value, your component then render the output.

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