[英]Assign a const variable inside a map function
I'm using Reactivesearch, and I'm trying to assign a const variable inside a map function.我正在使用 Reactivesearch,并且试图在 map 函数中分配一个 const 变量。 Specifically in the
.map
Like such:具体在
.map
像这样:
onAllData(data, streamData) {
return (
<div className="grid">
{
data.map((item) =>
const propertyName = item.productName;
<div className="flex-container card" key={item._id}>
<div className="content">
<p>{propertyName}</p>
</div>
</div>
)
}
);
}
const propertyName = item.productName;
is giving me problem.给我带来了问题。
Error states unexpected token
. Error states unexpected token
。
Is it possible?是否可以?
You need to go from expression syntax to the block syntax, using braces and return
:您需要使用大括号和
return
从表达式语法转到块语法:
data.map((item) => {
const propertyName = item.productName;
return (<div className="flex-container card" key={item._id}>
<div className="content">
<p>{propertyName}</p>
</div>
</div>)
})
However, you could also use destructuring to get your propertyName
, and then you are back to one expression:但是,您也可以使用解构来获取您的
propertyName
,然后您将返回到一个表达式:
data.map(({productName, _id}) =>
<div className="flex-container card" key={_id}>
<div className="content">
<p>{propertyName}</p>
</div>
</div>
)
That is an improper use of arrow functions.这是对箭头函数的不当使用。
Fix:使固定:
data.map(({productName, _id}) => (
<div className="flex-container card" key={_id}>
<div className="content">
<p>{productName}</p>
</div>
</div>
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.