从一组数据渲染 MUI 组件并以不同方式显示第一个组件

render MUI components from an array of data and display the first one differently

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

parent 组件的代码

const Sandbox = () => {

let summaryContents: any[] = [
    {
      Title: "Construction costs",
      TotalPrice: "000",
      Subcontents: [
        {
          Subtitle: "Sanitation",
          SubtitlePrice: "00",
        },
        {
          Subtitle: "PoolLights",
          SubtitlePrice: "00",
        },
        {
          Subtitle: "PoolCleaner",
          SubtitlePrice: "000",
        },
      ],
    },
    {
      Title: "Pool interior costs",
      TotalPrice: "000",
      Subcontents: [
        {
          Subtitle: "Title1",
          SubtitlePrice: "00",
        },
        {
          Subtitle: "Title2",
          SubtitlePrice: "000",
        },
        {
          Subtitle: "Title3",
          SubtitlePrice: "00",
        },
      ],
    },
];


  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}
    </>
  );
};

child 组件的代码

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>
  );
};

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

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

const Sandbox = () => {
  const summaryContents: any[] = [
    {
      Title: "Construction costs",
      TotalPrice: "000",
      Subcontents: [{
          Subtitle: "Sanitation",
          SubtitlePrice: "00",
        },
        {
          Subtitle: "PoolLights",
          SubtitlePrice: "00",
        },
        {
          Subtitle: "PoolCleaner",
          SubtitlePrice: "000",
        },
      ],
    },
    {
      Title: "Pool interior costs",
      TotalPrice: "000",
      Subcontents: [{
          Subtitle: "Title1",
          SubtitlePrice: "00",
        },
        {
          Subtitle: "Title2",
          SubtitlePrice: "000",
        },
        {
          Subtitle: "Title3",
          SubtitlePrice: "00",
        },
      ],
    },
  ];

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

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

您可以将 QuoteCard 放在外面,只将 Accordion 项目放在 child 组件中:

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>

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

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}
    />
  ))}
</>