In this post, we will see how to resolve ReactJS – Trying to access the title of an object I’m pulling from a webpage online, but getting these errors
Question:
I’m trying to create a news website with ReactJS and I’m getting three various warnings/errors when I try to run the page. I can get it to display the article link properly when I don’t include the title. I still get the two first warnings in this case, but when I try to access the article’s title it won’t load the page at all.Any help is greatly appreciated.
The warnings/errors:
Warning: Each child in a list should have a unique “key” prop.
Warning: Invalid value for prop
href
on tag.Uncaught Error: Objects are not valid as a React child (found: object with keys {rendered}). If you meant to render a collection of children, use an array instead.
LatestArticles.js
Best Answer:
Warning: Each child in a list should have a unique “key” prop.- when adding
keys
you need to add it to the parent element of the item (in this case theul
tag).
Warning: Invalid value for prop href on tag.
Uncaught Error: Objects are not valid as a React child (found: object with keys {rendered}). If you meant to render a collection of children, use an array instead.
For these two errors, it might be due to how you are fetching your data, but there are a few things I would look into first:
- You are using react hooks within a React hook (
useState
andReact.useState
), instead make the default an emptyarray
.
- Take your useEffect out of your function since it only runs when there are side effects… I would remove your function altogether and just stick to rendering your posts through the
useEffect
and that should work.
If you have better answer, please add a comment about this, thank you!
Source: Stackoverflow.com