在地图函数中使用三元运算符可同时渲染两个结果

丹尼尔·威廉姆斯

我正在创建一条垂直的时间线,当您沿时间线移动时,卡片的两边会交替出现,我正在尝试添加弹出效果,以显示有关填充时间线上相对空白的人物/事件的更多信息。

我正在尝试通过在地图回调中使用三元运算符(使用模数依次交替使用模数)来实现此目的,但是它呈现/返回了两种可能的Popover结果,onClick会在卡的两面弹出Popover。

render() {
    const cards = timelineObjects.map((card, i) => (
      <React.Fragment key={i}>

        {i % 2 === 0 ? (
          <VerticalTimelineElement
            className="vertical-timeline-element--work"
            key={i}
            iconStyle={{
              background: "rgb(40,49,72)",
              color: "#000"
            }}
            paddingTop="0em"

            //icon={<Print/>}
          >
            <div>
              <Card className="card">
                <CardActionArea>
                  <CardMedia
                    style={{ height: 0, paddingTop: "100%" }}
                    image={card.image}
                  />
                  <CardContent>
                    <Typography gutterBottom variant="h5" component="h2">
                      {card.title}
                    </Typography>
                    <Typography component="p">{card.subtitle}</Typography>
                  </CardContent>
                </CardActionArea>

                <Button
                  size="small"
                  color="primary"
                  component={Link}
                  //to={card.path}
                  onClick={this.handlePop}
                >
                  Learn More, index: {i}, RIGHT
                </Button>
                <Popover
                  open={this.state.popped}
                  anchorEl={this.state.anchorEl}
                  anchorOrigin={{
                    horizontal: "right",
                    vertical: "center "
                  }}
                  transformOrigin={{
                    horizontal: "right",
                    vertical: "bottom"
                  }}
                  onClose={this.handleRequestClose}
                >
                  Right popover text
                </Popover>
              </Card>
            </div>
          </VerticalTimelineElement>
        ) 
        : 

        (
          <VerticalTimelineElement
            className="vertical-timeline-element--work"
            key={i}
            iconStyle={{
              background: "rgb(40,49,72)",
              color: "#000"
            }}
            paddingTop="0em"

            //icon={<Print/>}
          >
            <div>
              <Card className="card">
                <CardActionArea>
                  <CardMedia
                    style={{ height: 0, paddingTop: "100%" }}
                    image={card.image}
                  />
                  <CardContent>
                    <Typography gutterBottom variant="h5" component="h2">
                      {card.title}
                    </Typography>
                    <Typography component="p">{card.subtitle}</Typography>
                  </CardContent>
                </CardActionArea>

                <Button
                  size="small"
                  color="primary"
                  component={Link}
                  //to={card.path}
                  onClick={this.handlePop}
                >
                  Learn More, index : {i}, LEFT
                </Button>
                <Popover
                  open={this.state.popped}
                  anchorEl={this.state.anchorEl}
                  anchorOrigin={{
                    horizontal: "left",
                    vertical: "center "
                  }}
                  transformOrigin={{
                    horizontal: "left",
                    vertical: "bottom"
                  }}
                  onClose={this.handleRequestClose}
                >
                  Left popover text
                </Popover>
              </Card>
            </div>
          </VerticalTimelineElement>
        )}
      </React.Fragment>
    ));

这是结果的屏幕截图。

布兰登

您的弹出窗口都锚定在同一元素(this.state.anchorEl)上,并且都配置为基于相同的布尔值(this.state.popped打开这意味着,如果您的时间轴中有2个以上的对象,则为每个对象渲染一个弹出框,所有弹出框将一起打开或关闭,并且所有弹出框均位于唯一锚元素(无论是哪个元素)的左侧/右侧。

您可能应该创建一个新TimelineObject组件,该组件呈现单个时间轴对象,并且可以具有自己的本地状态,并分配自己的本地状态anchorEl以将其弹出窗口锚定到。可能还有它自己的popped状态。那么您的地图函数将更像:

timelineObjects.map((card, i) => <TimelineObject key={i} card={card} onLeft={i%2==0} />)

另外,this.state.popped也可以将其用作显示索引框的卡片索引,而不是将其用作布尔值。并在您的Popover代码中执行:

<Popover open={this.state.popped === i} ...

而当您将其popped设置为this.setState({popped: indexOfCardToShowPopover, anchorEl: elementOfCardToAnchorPopover });

这样,一次只能打开一个弹出窗口。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在反应三元运算符中使用AND语句(两个条件)

JSX中的三元运算符内如何有两个函数调用?

使用三元运算符进行条件渲染

我可以将三元运算符与两个分别返回void的函数一起使用吗?

如何组合两个 JavaScript 三元运算符?

从if else语句到三元运算符的两个变量

三元运算符中的两个任务-PHP

?:三元运算符,后面有两个声明

在三元条件运算符中执行两个以上的操作

为什么具有两个常量的三元运算符比带有变量的三元运算符快?

使用一个三元运算符渲染多个组件

在回声中使用三元运算符?

在 Angular 的 *ngFor 中使用三元运算符

在groupby中使用三元运算符的Python

在freemarker中使用三元运算符?

在 MySQLi 查询中使用三元运算符

在C中使用三元运算符递增

在三元运算符中使用OR

在javascript中使用三元运算符

使用三元运算符的JavaScript递归函数

在具有JavaScript的函数中使用三元运算符

如何在函数的参数中使用php三元运算符?

如何在 JavaScript 中使用三元运算符更改函数中的 if else 语句?

使用三元运算符在Javascript中运行2个函数

在 Reactjs 18.0 中使用 jsx 内的三元运算符条件渲染状态值时出错

与三元运算符反应条件渲染

地图内的三元运算符

两个三元运算符分散为一种内联样式,显示“无法编译”错误

一行中的两个三元运算符导致“无法编译”错误?