What is the best approach when passing Objects through props?

Hi all,

I want to fetch all of the custom taxonomies and show them on a page.
However when i pass them as Objects i can’t use .map for each of them. Do you know what would be the best approach to loop them? (see Screenshots)

Many thanks.
All the best,
Dejan


Hi @dejangeorgiev

You can loop over the keys in an object with something like:

Object.keys(myObject).forEach( key =>  {...})
1 Like

Hello @mburridge,

Thank you very much for the help. However i was able to fix it with .map function, just a different approach.

Do you have any idea how to get the taxonomy description and i am sure it is included in the api?

Many thanks.
All the best,
Dejan

Hi @dejangeorgiev

Glad you managed to fix your issue.

Regarding the description I can confirm that it is sent from the WP REST API, and also that it isn’t showing in frontity.state.

I’m not sure why, but I’ve referred this to the dev team. I’ll let you know as soon as I hear back from them.

1 Like

Hi @mburridge

Thank you so much.
Looking forward to hear from you regarding the taxonomy description.

Best,
Dejan

Hi @dejangeorgiev

As I said earlier, I referred this issue to our dev team. Here is what they replied:

1 Like

Hello @mburridge

Thank you very much for the resolution. However when i try with this approach the script runs an unlimited number of requests and eventually crashes. :frowning:

And when i try to fetch the data only with the taxonomy, can´t get the description either.

Thank you very much for helping out.
All the best,
Dejan

@dejangeorgiev

Do you have a github repo of your project so we can take a look at your code? Thanks.

Hi @mburridge

Here you go: https://github.com/dejangeorgiev/ruthgeorgiev-frontity

Please note that currently the API is not working as i am taking some actions on the hosting, so you might not be able to reproduce it. It will be available from Monday again.

Thank you very much.
Best
Dejan

Hi @dejangeorgiev

Apologies for the delay in getting back to you. I just cloned your repo and everything seems to be working okay. I’m guessing you fixed your issue since your last post?

The compile took a bit longer than usual, but this may be due to the assets that you’re downloading.

Hi @mburridge

No worries at all. :slight_smile:
Yes, regarding of looping through the categories, i fixed it.
However for getting the description from the taxonomies i wasn’t able to fix it with the resolution you provided.
When i try actions.source.fetch("/category/example", { force: true });, the script runs an unlimited number of requests and eventually crashes. (see one of my previous answers above)

Let me know if you have any thoughts on that.

Many thanks.
All the best,
Dejan

Hey @dejangeorgiev!

Your problem regarding the infinite loop in the taxonomies seem to be that you are calling const TaxonomyData = actions.source.fetch('/cuisine'); inside of the body of the react component.

Calling fetch() will update the state, so when it finishes, it will re-render the GetTaxonomy component with new data, causing the actions.source.fetch('/cuisine') to be executed again, and again…

What you want to do is wrap the actions.source.fetch() with useEffect(), something like:

(disclaimer: this is not tested)

const Component = () => {
  useEffect(() => {
    actions.source.fetch(taxonomyLink);
  }, [])

  const data = state.source.get(taxonomyLink);

  if (!data.isReady) return null;

  const taxonomy = state.source[data.taxonomy][data.id];

  return <div>{taxonomy.name}: {taxonomy.description}</div>
}

EDIT: Fixed the code to use data.taxonomy.

1 Like

Hi @mmczaplinski,

Thank you very much for explaining the Issue well and providing a solution for it.
It works perfect as you described it. Only instead of
const taxonomy = state.source[data.type][data.id];
i must use
const taxonomy = state.source[data.taxonomy][data.id];

Thank you very much!
Best,
Dejan

3 Likes