I'm new to React.
I know that comments should be received as an object, so I tried using a for...in loop to iterate over them. However, when I attempted to use the for...in loop, I encountered an error:
{for (for Comment in commentData){
<div className="space-y-6">
<div className="border-b pb-6">
<div className="flex items-start gap-4">
<img
src={Nodejs}
alt="Nodejs"
className="w-10 h-10 rounded-full object-cover"
/>
<div className="flex-1">
<div className="flex items-center gap-3 mb-2">
<h3 className="font-semibold">{Comment.userName}</h3>
<span className="text-gray-500 text-sm">{Comment.date}</span>
</div>
<p className="text-gray-700">{Comment.comment}</p>
</div>
</div>
</div>
</div>
}}
The error message says:
Unexpected token: did you mean {}?
I believe the issue is with how I am using the for...in loop inside JSX. Can someone explain the correct way to iterate over commentData and why this error occurs? I'd also appreciate an explanation of how JSX handles loops and how to properly fix this issue.
for..initerates an object's keys which wouldn't typically have properties likeuserName,dateandcomment. What sort of object iscommentData?