Created
June 23, 2019 06:21
-
-
Save cdsandoval/b495cfd2b333e7feda6753e3b4c15263 to your computer and use it in GitHub Desktop.
How would you async fetch data for a React component
This file contains hidden or 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
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