score:1
Accepted answer
control your loader state with hooks and use .finally()
to convert your loading state back to false.
import { useeffect, usestate } from 'react';
export default function home() {
const [loading, setloading] = usestate(false);
useeffect(() => {
setloading(true);
fetch('/api/hello')
.then((res) => res.json())
.then((data) => {
// do something with data
})
.catch((err) => {
console.log(err);
})
.finally(() => {
setloading(false);
});
}, []);
if (loading) {
return <loadingcomponent />;
}
return <myregularcomponent />;
}
score:1
const [product,setproduct]=usestate({})
const [isloading, setloading]=usestate(false);
useeffect(()=>{
setloading(true);
fetch(`http://localhost:5000/readsinglecarsdata/${id}`)
.then(res=>res.json())
.then(data=> {
setproduct(data)
setquantity(data.quantity)
setloading(false);
});
},[]);
return isloading ? <spiner /> : <component />
try this
Source: stackoverflow.com
Related Query
- How to display a spinner when data is loading from server by fetch api in REACT JS?
- How to fetch and display data from Express API to a React app?
- React App: How to display data from api using fetch
- How to automatically fetch data from json server when data gets updated in it in react js
- How to properly fetch data from several API calls and display it to the DOM in React
- REACT JS How can I display my fetched API data from onClick?
- How to Fetch and Display an Image from an express backend server to a React js frontend?
- How to fetch and display data from json file in react typscript
- How to display data from api in React
- Getting undefined when trying to fetch data from an api - React
- React Hook to fetch data from APi when click 2 different button
- How to get React to re fetch my data from API after a POST or DELETE to Database?
- How to display data from the api in react
- How to display data from api in react component
- How to fetch data from a custom React hook (API) with onClick and display it in a Div using TypeScript?
- How do I display data from api using react and redux
- How to load and display data with React from an API running on localhost
- How can i add React loading spinner when item is fetching from database?
- How to fetch data from API and then pass it to another component in react after it is fully loaded?
- How to fetch sequence api calls where second api call need particular data from first api call result using react hooks?
- How to display data after getting complete data from API endpoint in react native?
- how to fetch data with react-app from node,express-server, when react-app is called from server
- How to fetch data from Nextjs API route when build and deploy?
- How to fetch data from api in React by using axois?
- I have a data coming from the api how do I display it in tree form in react
- How to fetch data from server using JavaScript? (adding backend to React app for the first time)
- How to set state at the server for data from API using react redux
- How to fetch data from API for multiple items in React JS component
- I'm fetching a problem when I'm trying to fetch data from an API by using useEffect in react
- How to fetch data from Web API using React query?
More Query from same tag
- Fire an event via click of an element
- getDisplayMedia does not record all audio output from my pc
- How can I test a switch case that depends on a parameter?
- How can I make onClick() not trigger when I click another onClick() inside of it?
- Django (DRF) trailing slash issue
- How to reference pdf.js library in React?
- Inline if-else with conditional operator not working React
- Change component state from another component
- Rendering a Google Map without react-google-map
- Call multiple `setState` hooks from multiple successive async calls in a for loop
- Use onFocus on a div with React and Typescript
- How to access cookie in server-side data fetch for isomorphic React app
- Can't place a background image in next JS and Tailwind CSS
- React Owl Carousal responsive not working in smaller screens
- React is not working on my machine
- Updating text after button click with react.js
- Can't pass a head of a method to event handling in React markup
- Update List via Redux's Reducer
- How can I solve 'ENOENT' error in next.js api section?
- ReactJS - response in async call
- Data Flow between two different react application
- React Typescript - Math.random gives wrong result even tho min value and max value is set
- React isn't updating a variable from a function
- How can I make my React app render Bootstrap tables with crud
- React Hooks Rendered more hooks than during the previous render
- Reactjs - Update options by hitting API on every input change in Autocomplete component (Material UI)
- Send data back to React client from Node server with POST request
- Two way binding between React components
- How to change the numbers in steps of material-ui Stepper?
- Adding External JS file to React JS Project