gpt4 book ai didi

javascript - Array.map 索引未定义?

转载 作者:行者123 更新时间:2023-12-05 09:32:43 25 4
gpt4 key购买 nike

我的 React 中有以下代码:

{
comments.map((comment, index) =>
console.log({index}),
<Textfield key = {index} placeholder = "Add Comment" Id = "addComment" / >
)
}

但是,这会返回错误,即 'index' is not defined no-undef on the console.log line。这是状态:

const [comments,setComments] = useState([[]])

为什么会这样?

最佳答案

您需要从 map 函数返回一个元素。在这里你只是尝试到底是什么?我认为您只需要用 { } 包装您的函数并使用 return 语句。

{
comments.map((comment, index) => {
console.log({index});
return <Textfield key = {index} placeholder ="Add Comment" Id="addComment" />;
})
}

关于javascript - Array.map 索引未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67754666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com