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

126
Views
How can i add loading spinner before fetching data in react?

What is the correct way to add a spinner while fetching data from API in react js? I tried to display the spinner until the data.length > 0 it works fine when the data is more than zero. But when it is actually zero, the loading spinner doesn't stop. Is there any good solution for this?

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

0

You didn't provide enough info or code to tell exactly what to do but i will give you a simple example

just create a state for the loading set it true and when the fetch is done set it false

I hope this example is clear

import { useEffect,useState } from 'react'

export default function Page() {
    const [data, setData] = useState(null)
    const [loading, setLoading] = useState(true)

    useEffect(() => {
        fetch('/api/test',{method: 'GET'})
        .then(res => res.json())
        .then(data => {
            console.log(data)
            setData(data)
        }
        )
        .catch(err => {
            console.log(err)
        }
        )
        .finally(() => {
            setLoading(false)
        })
    }, [])
    if(loading) return <div>Loading...</div> // or your loading spinner
    return (
    <div>{JSON.stringify(data)}</div>
  )
}

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!