How we can set loading in react
I tried using usestate but I don’t work
I want to set loading while getting data from api
Before getting data I set
const {loading, setloading} = useState (false)
const getdata = () => {
setloading (true);
fetch();
}
2
Answers
Check this snippet below:
Remember to replace the setTimeout function with your actual API call using a library like fetch or axios.
You could initialize the component in a loading state, then update the
loading
variable to befalse
within the callback of yourfetch
call.It would be a good idea to wrap your
fetch
call withinuseEffect
so it only runs on component load.