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

111
Vistas
useState element not rerendering when triggered by an event handler in a child component

I have a parent component with a useState that is being displayed. What I want to do is have a child component be able to update this state to change what the parent is displaying. I currently have the following:

function Parent() {

    const [myWindow, setMyWindow] = useState(null)
 
    useEffect(() => {

        setMyWindow(<Child updateWindowFunc={() => setMyWindow(someNewWindow)} />)    

    }, []}

    return (
        <div>
            {myWindow}
        </div>
    )

}


function Child({updateWindowFunc}) {
    //Somehow calls updateWindowFunc
}

(Note that my actual code is set up somewhat differently, so don't mind the syntax as much as the general concept.)

I've found that I can get the value of myWindow to change, but the actual display doesn't show any change. I've tried forcing a re-render after the change, and adding a useRef to display useRef.current, but nothing seems to update which window is actually being rendered.

What am I doing wrong?

Edit: I've found that it works if I switch to a different type of component, but if its just a different element of the same component then there is no re-render. I've been using React.createElement(), so I would think the 'objects' are distinct, but maybe I just misunderstand how this works.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Can you provide more details or working\not working code example that has more info?

Currently it's hard to get, cause that doesn't make any sense since you can just render it like below and that code will have same result as one that you are trying to run

function Parent() {

    return (
        <div>
            <Child />
        </div>
    )

}

So for better understanding and fixing a problem \ finding a different approach, please, provide more details

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