js async in useeffect code example
Example 1: react useeffect async
const MyFunctionnalComponent: React.FC = props => {
useEffect(() => {
// Using an IIFE
(async function anyNameFunction() {
await loadContent();
})();
}, []);
return <div></div>;
};
Example 2: async in useeffect
function myApp() {
const [data, setdata] = useState()
useEffect(() => {
async function fetchMyAPI() {
const response = await fetch('api/data')
response = await response.json()
setdata(response)
}
fetchMyAPI()
}, [])
}