【问题标题】:render MUI components from an array of data and display the first one differently从数据数组渲染 MUI 组件并以不同方式显示第一个组件
【发布时间】:2021-12-07 18:13:22
【问题描述】:

在这里,我试图通过将道具传递给组件的列表数组来呈现Accordion 的内容。我有点理解问题出在哪里,但我不知道我还能如何调用组件并传递道具。下面是我的代码。

父组件代码

const Sandbox = () => {

let summaryContents: any[] = [
    {
      Title: "Construction costs",
      TotalPrice: "$25000",
      Subcontents: [
        {
          Subtitle: "Sanitation",
          SubtitlePrice: "$5000",
        },
        {
          Subtitle: "PoolLights",
          SubtitlePrice: "$5000",
        },
        {
          Subtitle: "PoolCleaner",
          SubtitlePrice: "$15000",
        },
      ],
    },
    {
      Title: "Pool interior costs",
      TotalPrice: "$20000",
      Subcontents: [
        {
          Subtitle: "Title1",
          SubtitlePrice: "$5000",
        },
        {
          Subtitle: "Title2",
          SubtitlePrice: "$10000",
        },
        {
          Subtitle: "Title3",
          SubtitlePrice: "$5000",
        },
      ],
    },
];


  let summaryContentsList: any[] = [];

  summaryContents.forEach((item: any, index: number) => {
    summaryContentsList.push(
      <QuoteSummary
        
        Title={item.Title}
        TotalPrice={item.TotalPrice}
        Subtitle={item.Subcontents.Subtitle}
        SubtitlePrice={item.Subcontents.SubtitlePrice}
      />
    );
  });
  return (
    <>
      
      {summaryContentsList}
    </>
  );
};

子组件的代码

const QuoteSummary: FC<QuoteSummaryProps> = (props: QuoteSummaryProps) => {
  const classes = useStyles();
  return (
    <QuoteCard>
      <Typography variant="h1" sx={{ paddingBottom: 2 }}>
        Quote Summary
      </Typography>
      <DialogCloseButton onClose={clicked} />

      <Accordion
        className={classes.accordion}
        sx={{
          paddingTop: 0,
        }}
      >
        <AccordionSummary
          expandIcon={<ExpandMoreIcon />}
          aria-controls="panel1a-content"
          id="panel1a-header"
        >
          <Typography variant="h3">{props.Title}</Typography>
          <Typography variant="h3" sx={{ paddingLeft: 10 }}>
            {props.TotalPrice}
          </Typography>
        </AccordionSummary>
        <AccordionDetails>
          <Typography>{props.Subtitle}</Typography>
          <Typography>{props.SubtitlePrice}</Typography>
        </AccordionDetails>
      </Accordion>
    </QuoteCard>
  );
};

这就是它的外观,因为我只想渲染一次标题和下面的手风琴内容。 我必须遵循的方法是什么

【问题讨论】:

  • 这能回答你的问题吗? Rendering React Components from Array of Objects
  • 逻辑是存在的,但问题是我通过一个组件渲染它,所以我将父组件的道具传递给子组件。所以你提供的链接不能解决它@wowandy
  • @kevinTHEprogrammer 我不明白你的要求。你想让标题Quote Summary 只出现一次吗?那你为什么不把它放在外面呢?
  • @NearHuscarl 我在父组件 中渲染 组件。报价摘要标题位于 组件内。不知道在传递整个 组件时我应该如何将其从循环中取出。

标签: reactjs typescript foreach material-ui accordion


【解决方案1】:

你可以把QuoteCard放在外面,只把Accordion项放在子组件里:

const QuoteSummary: FC<QuoteSummaryProps> = (props: QuoteSummaryProps) => {
  return (
    <Accordion>
      {...}
    </Accordion>
  );
};
<QuoteCard>
  <Typography variant="h6" sx={{ paddingBottom: 2 }}>
    Quote Summary
  </Typography>
  <DialogCloseButton onClose={() => {}} />

  {summaryContents.map((item) => (
    <QuoteSummary
      Title={item.Title}
      TotalPrice={item.TotalPrice}
      Subtitle={item.Subcontents.Subtitle}
      SubtitlePrice={item.Subcontents.SubtitlePrice}
    />
  ))}
</QuoteCard>

编辑:另一种方法是根据父组件提供的道具有条件地在子组件中呈现标题:

const QuoteSummary: FC<QuoteSummaryProps> = (props: QuoteSummaryProps) => {
  return (
    <QuoteCard>
      {props.displayTitle && (
        <Typography variant="h6" sx={{ paddingBottom: 2 }}>
          Quote Summary
        </Typography>
      )}
      <DialogCloseButton onClose={() => {}} />
      <Accordion>
        {...}
      </Accordion>
    </QuoteCard>
  );
};
<>
  {summaryContents.map((item, i) => (
    <QuoteSummary
      displayTitle={i === 0}
      Title={item.Title}
      TotalPrice={item.TotalPrice}
      Subtitle={item.Subcontents.Subtitle}
      SubtitlePrice={item.Subcontents.SubtitlePrice}
    />
  ))}
</>

【讨论】:

  • 它可以工作,我已经知道了,但我希望它在组件内?还有其他我们可以做到的吗? @NearHuscarl
  • 如果不行就想知道有没有其他办法
  • @kevinTHEprogrammer 为什么你希望它在组件内部呢?你还有其他组件在玩吗?
  • @kevinTHEprogrammer 查看我的更新答案。
【解决方案2】:

您可以为数组项创建一个渲染函数并像这样使用它们:

const Sandbox = () => {
  const summaryContents: any[] = [
    {
      Title: "Construction costs",
      TotalPrice: "$25000",
      Subcontents: [{
          Subtitle: "Sanitation",
          SubtitlePrice: "$5000",
        },
        {
          Subtitle: "PoolLights",
          SubtitlePrice: "$5000",
        },
        {
          Subtitle: "PoolCleaner",
          SubtitlePrice: "$15000",
        },
      ],
    },
    {
      Title: "Pool interior costs",
      TotalPrice: "$20000",
      Subcontents: [{
          Subtitle: "Title1",
          SubtitlePrice: "$5000",
        },
        {
          Subtitle: "Title2",
          SubtitlePrice: "$10000",
        },
        {
          Subtitle: "Title3",
          SubtitlePrice: "$5000",
        },
      ],
    },
  ];

  const renderContent = (item: any) => (
     <QuoteSummary
        Title={item.Title}
        TotalPrice={item.TotalPrice}
        Subtitle={item.Subcontents.Subtitle}
        SubtitlePrice={item.Subcontents.SubtitlePrice}
      />
  );

  return (
    <>
      {summaryContents.map(renderContent)}
    </>
  );
};

【讨论】:

  • 感谢您的回答,但实际上并没有奏效。在这里,您使用array.map 完成的操作与我使用arrayList 和foreach 循环完成的操作完全相同。这里的问题是标题“报价摘要”是子组件的一部分。所以我只需要传递数组中的手风琴内容。如果我必须通过道具,不知道该怎么做?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-10
  • 1970-01-01
  • 2020-07-14
  • 2020-08-09
  • 2018-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多