[英]React - Conditional Element in a map Function
I want to display a different link in this map function, If x is for example 3 i want to show <Link>Put</Link>
otherwise i want to show <Link>Remove</Link>
.我想在这个地图函数中显示一个不同的链接,如果 x 是例如 3 我想显示
<Link>Put</Link>
否则我想显示<Link>Remove</Link>
。 I am trying to do some conditional but i get errors, how can i write it down?我正在尝试做一些有条件的操作,但出现错误,我该如何写下来?
const mapWorkplace = (arr: SuggestedWorkplace[], deallocation: boolean) =>
arr.map((x, i) => ({
...x,
action: <Link>Put</Link> || <Link>Remove</Link> ,
__props: {
style: { background: !(i % 2) ? "#fff" : "rgba(233, 249, 249, .6)" }
}
}));
Assuming your error comes from the value of action
, you can do it using a conditional operator :假设您的错误来自
action
的值,您可以使用条件运算符来做到这一点:
const mapWorkplace = (arr: SuggestedWorkplace[], deallocation: boolean) =>
arr.map((x, i) => ({
...x,
action: x === 3 ? <Link>Put</Link> : <Link>Remove</Link> ,
__props: {
style: { background: !(i % 2) ? "#fff" : "rgba(233, 249, 249, .6)" }
},
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.