Skip to content

Instantly share code, notes, and snippets.

@cdsandoval
Created June 23, 2019 06:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdsandoval/b495cfd2b333e7feda6753e3b4c15263 to your computer and use it in GitHub Desktop.
Save cdsandoval/b495cfd2b333e7feda6753e3b4c15263 to your computer and use it in GitHub Desktop.
How would you async fetch data for a React component
Using hooks with useEffect like this :
useState(async ()=>{
const response = await fetch(url);
const json = await response.json();
setData(json);
}
- with Classes using compenentDidMount or willMount :
async componentDidMount(){
const response = await fetch(url)
const json = await response.json()
this.setState({data:json));
}
- With Redux using redux-thunk would be :
function fetchData(){
return async dispatch => {
const response = await fetch(url);
dispatch( response );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment