Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

99
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!