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

212
Views
React useEffect execute initial setup

I have a parent component that always needs to retrieve a token first before the subcomponents get executed. As discussed in this post What is the correct order of execution of useEffect in React parent and child components? the subcomponents useEffect gets executed first.

My question is how can I handle retrieving the token on initial loading (in the main component)?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I solved it by using a state to track if the session has been initialized. If the session has not been initialized its displaying a loading animation. This is only executed on the initial loading of the app. The loading component can't have a useEffect that requires the api with a valid token or user information.

function App() {
  const [hasSession, setSession] = useState(false)

  useEffect(() => {
    async function initUserProfile() {
      await apiService.init()
      setSession(true)
    }

    initUserProfile()
  }, [])

  if (hasSession === false) {
    return (<Loading />)
  }

  return (
    <Router>...</Router>
  )
 }

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