r/reactjs • u/dance2die • Jan 01 '21
Needs Help Beginner's Thread / Easy Questions (January 2021)
Happy 2021!
Previous Beginner's Threads can be found in the wiki.
Ask about React or anything else in its ecosystem :)
Stuck making progress on your app, need a feedback?
Still Ask away! Weβre a friendly bunch π
Help us to help you better
- Improve your chances of reply by
- adding a minimal example with JSFiddle, CodeSandbox, or Stackblitz links
- describing what you want it to do (ask yourself if it's an XY problem)
- things you've tried. (Don't just post big blocks of code!)
- Format code for legibility.
- Pay it forward by answering questions even if there is already an answer. Other perspectives can be helpful to beginners. Also, there's no quicker way to learn than being wrong on the Internet.
New to React?
Check out the sub's sidebar! π
For rules and free resources~
Comment here for any ideas/suggestions to improve this thread
Thank you to all who post questions and those who answer them. We're a growing community and helping each other only strengthens it!
25
Upvotes
View all comments
Show parent comment
1
u/yluksim Jan 15 '21
From what I understand you want to grab a characters info, their planet, and their species, have that data available in one object, and that gets stored in an array of these objects.
I believe youre looking for Promise.all (which you are already using in the second code example) and Promise chaining. See these links:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
https://javascript.info/promise-chaining
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve
With promise.all, you could grab all the data at once, loop through the people, and then go find their corresponding planet and species in the data you grabbed (this would prevent repeating api calls for planet/species if you were making 2 calls for every character). I think there are something like 60 planets and 80 characters, so I dont think its a huge deal to just grab everything at once, especially if youre going to do every character (Id assume all the races would get used and most of the planets). Problem is, the SWAPI api has paginated data. In my example I used a recursive function to grab all the paged data. This is a little over complicated but for your example its necessary as you need all person, species, and planet information to construct the character cards.
Here is a good article on recursive fetch/api calls: https://codingwithmanny.medium.com/recursive-http-requests-in-javascript-44ea4beb74b5.
Here is an example of this working: https://codesandbox.io/s/fast-surf-gu76s
It grabs all person, planet, and species data, maps the species and planet to all characters, and then displays that array in a list.
I used the Javascript FetchAPI (https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) in my example instead of axios.js which is my preference but it doesnt matter which you use.
Hopefully this helps, let me know if you have any questions!
(feedback is welcome)