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

97
Visualizações
Render not waiting for componentdidmount function

I have a componentDidMount function which checks wether the user is logged or not by calling an API

This is my summed up code layout:

state = {
    login: false
};       

componentDidMount() {
    fetch('api/login/userislogged', {
        method: 'GET'
    })
    .then(response => response.json())
    .then(data => {
        this.setState({ login: data })
    })
}

 render() {
    return (
        <div>
            {!this.state.login ? <LoginComponent childToParent={this.childToParent}/>
                :
                <>
                    <NavMenu/>
                    <Container>
                        ...
                        ....
                    </Container>
                </>
            }
        </div>
    );
}

// this function gets back the login form response to set the login state to true if it was successful
childToParent = (childData) => {

    if (childData) {
        this.setState({ login: true })
    }
    else {
        this.setState({ login: false })
    }

};
   

The problem is that when the page first loads and the user is logged, the state.login is false, so for a brief millisecond it shows the login form, then when the function gets the api result back and sets the state.login to true, the component re-renders the page correctly without the login form.

Is there any better solution? thanks for the answers <3

about 4 years ago · Juan Pablo Isaza
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