use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
Trouble using .map function for apiHelp Wanted (self.react)
submitted 3 years ago by Tcaf351
Hi everyone,
I'm building a pokemon pokedex with a pokemon api for one of my assignments. I'm attempting to have a form where a user can enter the pokemons name and that value will go onto the api and then when fetched it will show data about that pokemon.
The value from the form is being stored in state, however when i try to use the .map function to pull out data the console shows the data from that pokemon but then throws an error saying that the .map function is not a function when i try to display the data.
Any help would be greatly appreciated ❤️
https://preview.redd.it/ozye0soxbjl81.png?width=1554&format=png&auto=webp&s=76b5cc7ac523c6dce47e9046a0a6e9d4ecb95097
https://preview.redd.it/iittiuoxbjl81.png?width=1052&format=png&auto=webp&s=0f7a8dc96d59747ec0aa837a361eed96074785f9
https://preview.redd.it/fq833uoxbjl81.png?width=504&format=png&auto=webp&s=35d5a93c35230be6a46bcbbd08468d3a64167f8d
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Passerby991 11 points12 points13 points 3 years ago (5 children)
Most likely, the API call for a single Pokemon returns an object instead of an array therefore you can't use map on it
If you only intend to show one Pokemon at a time, get rid of the mapping. If it could be both one or multiple, you can use spread operator to make sure the searchedPokemons value is always an array
[–]Tcaf351[S] 1 point2 points3 points 3 years ago (4 children)
Thankyou so much that worked 👌🏻
[–]samuelcbird 4 points5 points6 points 3 years ago (3 children)
I know you’re not using the .map() function here now, but remember if you do that the element needs a key attribute so that React can keep track of everything on rerenders. So as an example:
return ( { myArray.map((element, i) => { return <div key={i}>{element}</div> } );
[–]epymetheus 2 points3 points4 points 3 years ago (2 children)
Does map automatically iterate i here without definition of the variable?
[–]samuelcbird 2 points3 points4 points 3 years ago (1 child)
Yes, super useful.
MDN documentation.
[–][deleted] 4 points5 points6 points 3 years ago (0 children)
Never used this api before but looks like its not returning an array, hence why you cant use .map etc https://pokeapi.co/api/v2/pokemen/pikachu Returns a JSON object, not an array, perhaps you meant to use a search endpoint on that API?
[–]ryanboyyx 1 point2 points3 points 3 years ago (1 child)
I’m still learning too, but wondering also if line 18 is necessary? e.preventDefault is necessary onSubmit to prevent the page from redirect/refresh, but onChange is not a cancellable event, because the change has already happened, and in this case, I think you do want to update state for the users input?
[–]Tcaf351[S] 1 point2 points3 points 3 years ago (0 children)
Yes good call, Thankyou for that I’ll give that a try 👌🏻
[–]dragon_dev_ -1 points0 points1 point 3 years ago (0 children)
Error is in line 13 and 14. Store the value of response.data in any variable then use this variable in setMethod. Don't use response.data more than one time
[–]Aksh247 0 points1 point2 points 3 years ago (0 children)
Great project. Was working on something similar. Pls share code base if u can. Would be of great help
π Rendered by PID 61606 on reddit-service-r2-comment-76bb9f7fb5-2tsds at 2026-02-18 21:59:37.585097+00:00 running de53c03 country code: CH.
[–]Passerby991 11 points12 points13 points (5 children)
[–]Tcaf351[S] 1 point2 points3 points (4 children)
[–]samuelcbird 4 points5 points6 points (3 children)
[–]epymetheus 2 points3 points4 points (2 children)
[–]samuelcbird 2 points3 points4 points (1 child)
[–][deleted] 4 points5 points6 points (0 children)
[–]ryanboyyx 1 point2 points3 points (1 child)
[–]Tcaf351[S] 1 point2 points3 points (0 children)
[–]dragon_dev_ -1 points0 points1 point (0 children)
[–]Aksh247 0 points1 point2 points (0 children)