1

I'm trying to do a simple task. Rendering contents of an array:

const [comments, setComments] = useState([]);   // an array to begin with


handleFetch = () => {
   ...
   ...
   const obj = resp.data.comments;
   const new_array = new Array();
   new_array.push(obj);
   setComments(new_array);
   ...
}


return (

    {comments.forEach(comm => <p>{comm.post}</p>)}
);

comments (resp.data.comments) come in as an array of objects:

comments = [{'post': 'something', 'timestamp': 'some time'}]

Error output I'm getting:

Error: Objects are not valid as a React child (found: object with keys {post, timestamp}). If you meant to render a collection of children, use an array instead. (But I'm using an array. An array of objects)

5
  • these two steps: const new_array = new Array(); new_array.push(obj); are not necessary.. it will create a nested array Commented Apr 25, 2021 at 6:44
  • I've tried to go without it but got same error. This is what I did before: setComments(obj); Commented Apr 25, 2021 at 6:45
  • Replace forEach with map. ForEach does not return anything whereas map will return you an array with items that needs ti be rendered Commented Apr 25, 2021 at 6:47
  • .forEach() returns undefined. Also, you've wrapped your .forEach() in {} but without <> </>, causing it to be treated as an object initializer. So I suspect your return is returning { undefined: undefined }, thus the reason for the error message. Commented Apr 25, 2021 at 6:47
  • obj = resp.data.comments; makes no sense. I would guess data.comments is an array by itself and not a single comment Object. log it and check. Commented Apr 25, 2021 at 6:53

2 Answers 2

3

Since resp.data.comments is an array, you can directly set to it to state comments using setComments(resp.data.comments) and use Array.prototype.map function in jsx to render.

const [comments, setComments] = useState([]);   // an array to begin with


handleFetch = () => {
   ...
   ...
   setComments(resp.data.comments);
   ...
}


return (
<>
    {comments.map(comm => <p>{comm.post}</p>)}
</>
);
Sign up to request clarification or add additional context in comments.

2 Comments

Mark has mentioned in his question. comments (resp.data.comments) come in as an array of objects: the data as comments = [{'post': 'something', 'timestamp': 'some time'}]
Right. I'm voting up.
1

You need to use .map() to return the value. So you can fix like this:

return (
  <>
    {comments.map((comm, index) => <p key={index}>{comm.post}</p>)}
  </>
);

6 Comments

Why the need for key prop is true, this has nothing to do with the problem, and therefore is not a valid answer. Should have been a comment
I've read it multiple times, and still, your "answer" has nothing to do with the problem. Trust me, I've been doing React for years.
The problem is using forEach on return. You make sure using forEach like this work well?
This is indeed a part of the problem, but not the root cause. See the other answer for the actual problem. The iterated data is not an Array of Objects but an Array of a single Array of Objects
I saw. But why you told my answer nothing to do?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.