GET | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

GET

How can I display data from API in my React component? I use React + Redux

17th Mar 2020, 4:17 AM
Oleg
Oleg - avatar
2 Answers
+ 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}/> });
17th Mar 2020, 4:40 AM
Raj Chhatrala
Raj Chhatrala - avatar
0
thank you!
17th Mar 2020, 11:57 PM
Oleg
Oleg - avatar