Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

3
[NextJS] Fetching data from another fetched data
Post Flair (click to view more posts with a particular flair)
Author Summary
JedaFTW is in NextJS
Post Body

Hello there. So here's my problem: I'm trying to make a pokedex with pokeAPI and NextJS, my problem right now is I cant' fetch the abilities and description.

I fetch the pokemon data and that gives me among other data, an array with a name and url, like so:

"abilities": [
    {
      "ability": {
        "name": "limber",
        "url": "https://pokeapi.co/api/v2/ability/7/"
      },
      "is_hidden": false,
      "slot": 1
    },
    {
      "ability": {
        "name": "imposter",
        "url": "https://pokeapi.co/api/v2/ability/150/"
      },
      "is_hidden": true,
      "slot": 3
    }
  ]

So now i want to make another react component with these abilities and fetching from them I get its description. My problem is, I can't make it render to the page. I get the info, Already formatted as I want, but due to promises fullfilment time I cant get them rendered (I think)
My code to fetch each ability and get them on an array is this:

import { useEffect, useState } from 'react'
import { capitalize } from 'utils/capitalize'
import { filterEnglishAbility } from 'utils/filterEnglishAbility'

async function fetchAllAbilities(abilitiesUrls) {
  let abilitiesArr = []
  Promise.all(abilitiesUrls.map((url) => fetch(url)))
    .then((responses) => Promise.all(responses.map((res) => res.json())))
    .then((res) =>
      res.forEach((res) => {
        const abilityName = res.name
        const abilityEnglish = filterEnglishAbility(res.effect_entries)
        const abilityDesc = abilityEnglish[0].effect
        const abilityToAdd = `${capitalize(abilityName)}: ${abilityDesc}`
        abilitiesArr.push(abilityToAdd)
      })
    )
    .catch((error) => {
      console.log(error)
    })
  return abilitiesArr
}

export const useFetchAbilities = (abilitiesObj) => {
  const [abilitiesFetched, setAbilitiesFetched] = useState([])
  const abilitiesUrls = abilitiesObj.map((ability) => ability.ability.url)

  useEffect(() => {
    fetchAllAbilities(abilitiesUrls)
      .then((res) => {
        setAbilitiesFetched(res)
      })
      .catch((err) => console.log(err))
  }, [abilitiesObj])

  return abilitiesFetched
}

To that abilitiesFetched array I'll map each ability to a <p> tag to display. The code in GitHub is:

https://github.com/ValentinGTrapaga/pokedex-nextjs

Author
Account Strength
80%
Account Age
11 years
Verified Email
Yes
Verified Flair
No
Total Karma
97
Link Karma
51
Comment Karma
46
Profile updated: 3 days ago
Posts updated: 1 year ago

Subreddit

Post Details

Location
We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
2 years ago