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

137
Views
Setting state from an API call inside a useEffect hook

I am trying to set the state from an async API call and then dynamically render some JSX based on the returned data.

The problem is, I think the state is being set only after the JSX has already rendered, and since it is in an a useEffect hook with a dependency( "[]" ) it only renders on the initial load. Therefore, the map in the JSX does not run.

I know the API is returning the correct data, as I can tell from the console.log but not sure where to go from here? I've tried adding the workspaces variable as a dependency to the useEffect hook but that just causes an infinite loop. Any ideas?

export const WorkspaceToggle = () => {
 
    const [workspaces, setWorkspaces] = useState([])

    useEffect(() => {

      (async () => {
        
        const data = await getUserWorkspaces()
        setWorkspaces(data)
        console.log(data)
        
      })();

    }, []);
    
    return (
      <>
        <Container>
          <DropdownCon>
            <DropdownToggle>{dropdownText}</DropdownToggle>
            <DropdownMenu>
              
              {workspaces.map((space: any) => {
                
                <DropdownItem eventKey="1">{space.name} Workspace</DropdownItem>
                
              }) as ReactNode}
              
            </DropdownMenu>
          </DropdownCon>
        </Container>
      </>
    )
  }


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!