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)
const new_array = new Array(); new_array.push(obj);are not necessary.. it will create a nested arraysetComments(obj);.forEach()returnsundefined. Also, you've wrapped your.forEach()in{}but without<> </>, causing it to be treated as an object initializer. So I suspect yourreturnis returning{ undefined: undefined }, thus the reason for the error message.obj = resp.data.comments;makes no sense. I would guessdata.commentsis an array by itself and not a single comment Object.logit and check.