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

110
Visualizações
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 Respostas
Responde à pergunta

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 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