0
GET
How can I display data from API in my React component? I use React + Redux
2 ответов
+ 2
using fetch() API and promise.
For ex :
step 1: create a state using useState() hook.
const [userData, setUserData] = useState([]);
step 2(optional): if you want data to be fetched on click of a button, make a button and write a function for it's onClick={} event.
step 3:
const getData = () => {
fetch("https://api.fakeurl.com/users)
.then(res => res.json())
.then(data => setUserData(...userData, data));
}
and now simply run a map over the array and return component you wanna display.
userData.map(data => {
return <Component name={data.name}/>
});
0
thank you!