如何为旋转木马映射一组子项?

IT技术 javascript reactjs
2021-05-23 00:53:49

我正在尝试将 Carousel 组件包装在一组映射对象周围,作为组件的子组件。目前我只能让映射创建映射对象的 1 个子项。

Carousel 需要像这样:

<Carousel>
    <div>1</div>
    <div>2</div>
    <div>3</div>
</Carousel>

但在这个上不能正常工作

<Carousel>
    <div>
        <div>1</div>
        <div>2</div>
        <div>3</div>
    </div>
</Carousel>

请查看我的旋转木马环绕映射项目的 1 个子级的原始可重现代码:https : //codesandbox.io/s/blissful-elion-993vy 注意您会看到旋转木马工作正常,但许多旋转木马属性根本不起作用,因为它围绕着一个孩子,例如动画和指标props。

我试过这样做:

          <Carousel
            autoPlay={false}
            onClick={handleChangePage}
            next={loopNext}
            prev={loopPrev}
            NextIcon={<ArrowForwardIosRoundedIcon fontSize='large'/>}
            PrevIcon={<ArrowBackIosRoundedIcon fontSize='large'/>}
            navButtonsProps={{      
              style: {
                  backgroundColor: 'transparent',
                  color:"#447CF0"
              }
            }} 
            indicators={true}
            swipe={true}
            animation="slide"
          >
              {data[0]?.slice((page - 1) * itemsPerPage, page * itemsPerPage)
                .map((data, index) =>
                {
                  return (  
                    <Container key={index} maxWidth="lg" component="main">
                      <Grid container  key={index}>
                        <Grid item key={index} xs={4} md={4}>
                            <Card>
                            <CardHeader
                            title={<Chip label={data.symbol} />} 
                            subheader={data.adj_close}        
                            />      
                            <CardContent >
                              <MiniGraphs
                                historicalPrice={historicalPrice.filter(i => i.symbol === data.symbol)}
                                dateRange={date}
                              />            
                            </CardContent>
                            </Card>
                        </Grid>
                      </Grid>
                      </Container>
                    );
                })}
          </Carousel>

但是 Carousel 不会重新产生预期的结果。当我设置了每张幻灯片的 3 个孩子的数量时,它只会显示每张幻灯片 1 个孩子。

我想我也将项目数组切成 3 个项目的切片,然后将每个切片映射到 <Container> --> <Grid container> --> <Grid item/> --> ...

目前停留在如何实现这一点上。

编辑:我修复了沙箱。

编辑:所需的输出是让所有图形正确显示为数组中的多个子项。正如您在下图中所看到的,每张幻灯片只有一个孩子在填充 Carousel(我认为是因为我需要明确地映射一组孩子?)。注意到旋转木马下方的指示器(灰色圆圈)了吗?它们需要出现,否则它仍然是图形的一个大孩子。 在此处输入图片说明

2个回答

更新的答案:

轮播要求在开始时渲染所有字段。

要在每张幻灯片上创建包含 3 个项目的多张幻灯片,您应该将数据数组“拆分”为多个部分,每个部分包含 3 个项目。

对于这些项目,您可以<Container>像这样渲染一个包含 3 个孩子的单曲

const array_chunks = (array, chunk_size) => Array(Math.ceil(array.length / chunk_size)) .fill() .map((_, index) => index * chunk_size) .map((begin) => array.slice(begin, begin + chunk_size)); 
const chunks = array_chunks(SymbolData, 3);
<Carousel>
    {chunks.map((chuk) => {
        return (
            <Container>
                {chuk.map((c) => {
                    return (
                        <Grid item xs={4} md={4}>
                            <Card>
                                <CardHeader/>
                                <CardContent>
                                </CardContent>
                            </Card>
                        </Grid>
                    );
                })}
            </Container>
        );
    })}
</Carousel>

笔记:

  • 我使用以下 Stack Answer 将数据数组拆分为 3 个块:

    将数组拆分为块

  • <container>不创建行,它在一列中。我想你应该能够解决这个问题,我不太熟悉material-ui

沙盒


原答案:

您正在<container>为每个地图迭代渲染一个

将地图移动到里面<container>这样你就可以在单个中渲染多个“单元格”,<container>如下所示:

<Container  maxWidth="lg" component="main">
    <Grid container>
        {SymbolData?.slice((page - 1) * itemsPerPage, page * itemsPerPage).map(
            (data, index) => {
                return (
                    <Grid item key={index} xs={4} md={4}>
                        <Card>
                            ...
                        </Card>
                    </Grid>
                );
            }
        )}
    </Grid>
</Container>

更新的代码和盒子

根据我对问题陈述的理解,有两种可能的事情你想要实现一个。

第一个:如果你想在一张幻灯片中显示所有三个图形,你应该像上面的解决方案和这个CodeSandbox一样将你的symbolData地图移动到里面 因为你已经用页码切片了,你会看到 n 个不同的带有 3 个图形的幻灯片每张幻灯片基于页数。<Grid container>

第二个(不太可能):您可能希望以幻灯片的全宽显示图表。为此,您需要xs={12} md={12}<Grid item key={index} xs={?} md={?}>