I have an app that fetches data from an api. Whilst fetching there is a loading state that displays an icon to the screen. However this is a quick flash. I would like to display the loading icon on screen for a 2 seconds to improve UI and let the user know something is happening.
Here is my code:
const [info, setInfo] = useState({});
const [loading, setLoading] = useState(false);
useEffect(() => {
setLoading(true);
axios
.get(`https://restcountries.eu/rest/v2/alpha/${code}`)
.then((response) => {
console.log(response);
setInfo(response.data);
setLoading(false);
});
}, []);
return (
<div>
<Link to='/'>
<button>Back</button>
</Link>
{loading ? (
<LoadingIcon />
) : (
<CountryInfo
info={info}
borders={borders}
languages={languages}
currencies={currencies}
/>
)}
</div>
);
};