Created
March 17, 2023 03:33
-
-
Save pjcodesjs/55b4540aee4e1974c7c169e5d84f3a85 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from 'react'; | |
function MyComponent() { | |
const [data, setData] = useState(null); | |
useEffect(() => { | |
const fetchData = async () => { | |
const response = await fetch('https://myapi.com/data'); | |
const json = await response.json(); | |
setData(json); | |
}; | |
fetchData(); | |
}, []); | |
return ( | |
<div> | |
{data ? ( | |
<div>{data.message}</div> | |
) : ( | |
<div>Loading data...</div> | |
)} | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment