Created
September 30, 2020 17:50
-
-
Save matiasfha/c157600f6e7ca3f93ebe622c194eabc6 to your computer and use it in GitHub Desktop.
useState, useEffect, fetch ejemplo
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
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