Skip to content

Instantly share code, notes, and snippets.

@matthewholliday
Created November 20, 2021 03:43
Show Gist options
  • Save matthewholliday/2a417d7c69867ab79046cd6183dab041 to your computer and use it in GitHub Desktop.
Save matthewholliday/2a417d7c69867ab79046cd6183dab041 to your computer and use it in GitHub Desktop.
Use fetch with await/async in react
const [testData,setTestData] = useState({message : "abc"});
useEffect(() => {
async function setData(){
const response = await fetch('http://localhost:8080/test');
const parsedData = await response.json();
setTestData(parsedData);
setData();
},[]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment