Skip to content

Instantly share code, notes, and snippets.

@matiasfha
Created September 30, 2020 17:50
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 matiasfha/c157600f6e7ca3f93ebe622c194eabc6 to your computer and use it in GitHub Desktop.
Save matiasfha/c157600f6e7ca3f93ebe622c194eabc6 to your computer and use it in GitHub Desktop.
useState, useEffect, fetch ejemplo
const DummyComponent = () => {
const [ data, setData ] = React.useState([]) // data inicialmente será un arreglo vacio
React.useEffect(() => {
const requestData = async () => {
const respone = await fetch('http://someurl/request.json')
const json = await response.json()
setData(json.data) // Aquí se actualiza el estado con un nuevo arreglo que proviene desde el response
}
}, []) // Ya que no hay dependencias este arreglo va vacío y el efecto se ejecutará sólo una vez.
return <ul>{data.map(item => {
<li key={item.id}>{item.title}</li>
})}</ul>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment