r/learnreactjs • • Oct 11 '22

[NextJS] Fetching data from another fetched data

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

3 Upvotes

4 comments sorted by

View all comments

Show parent comment

1

u/JedaFTW Oct 12 '22

Yes, also my component was rerendering like crazy, because it didnt keep the value memoized. So I used useMemo hook like u/PM_ME_SOME_ANY_THING suggested and everything worked

1

u/link3333 Oct 12 '22

The dependency array argument of useEffect and useMemo should be doing a shallow comparison of the values provided.

For primitive types like numbers and strings, it will work perfectly fine. For reference types like objects and strings, it just checks if the reference is different. If you are making the object within the function component and the component re-renders, that's a new object and new reference. So the new object will not the be same as the last one passed to useEffect, and callback argument will be called.

  • Component renders
  • New object is created
  • useEffect is called due to it being a new reference
  • setAbilitiesFetched is called after the promise is done
  • setAbilitiesFetched caused a state change, and re-renders the component
  • Repeat the render

The switch to useMemo with an empty dependency array argument will allow that to run once. React will save onto the reference and return it on each render.

Note that in some older React code (React.memo), it allowed specifying the comparison method. So you could swap with a deep comparison method. I don't think they wanted to complicate thing with their newer hooks. You can technically JSON.stringify your object within the dependency array to force a string comparison, but the useMemo way is cleaner and should be doing much less work if the component re-renders often.

1

u/JedaFTW Oct 12 '22

Thanks a lot, really helpful your explanation