React-地图函数中的条件元素

问题描述 投票:0回答:1

我想在此地图功能中显示其他链接,如果x例如3,我想显示<Link>Put</Link>,否则我想显示<Link>Remove</Link>。我正在尝试执行一些有条件的操作,但出现错误,如何记录下来?

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)" }
    }
  }));
javascript reactjs conditional-statements
1个回答
0
投票

假设您的错误来自action的值,则可以使用conditional operator

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)" }
    },
}));
© www.soinside.com 2019 - 2024. All rights reserved.