Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

152
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda