I have the following code in my React:
{
comments.map((comment, index) =>
console.log({index}),
<Textfield key = {index} placeholder = "Add Comment" Id = "addComment" / >
)
}
However, this returns me error that 'index' is not defined no-undef
on the console.log line. This is the state:
const [comments,setComments] = useState([[]])
Why does this happen?
You need to return an an element from map function. Here you just try what exactly? I think you just need to wrap your function with { }
and use return
statement.
{
comments.map((comment, index) => {
console.log({index});
return <Textfield key = {index} placeholder ="Add Comment" Id="addComment" />;
})
}
Maybe you are just missing some curly braces around your map function
{
comments.map((comment, index) => {
console.log(index);
return <Textfield key={index} placeholder="Add Comment" Id="addComment" />;
})
}
you need to add { } in your array function
{
comments.map((comment, index) => {
console.log(index),
return <Textfield key = {index} placeholder = "Add Comment" Id = "addComment" / >
)}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.