简体   繁体   English

在 React JSX 中运行循环并应用条件

[英]Run a loop and apply condition in React JSX

I have written a React component which will render Material UI Cards in a Grid.我编写了一个 React 组件,它将在网格中呈现 Material UI Cards。 In one row there would be 4 cards.一排会有4张牌。 If total number of cards is divisible by four then it is not problem but how can I add the condition to check and render cards if it is not divisible by 4. I am writing functions to render 3 cards, 2 cards or 1 card depending on what is left in the last.如果卡的总数可以被 4 整除,那么这不是问题,但是如果它不能被 4 整除,我该如何添加条件来检查和渲染卡。我正在编写函数来渲染 3 张卡、2 张卡或 1 张卡,具体取决于最后还剩下什么。

This is my React code and I need to optimise to render elements in grids.这是我的 React 代码,我需要优化以呈现网格中的元素。

 export default function NestedGrid(props) { const classes = useStyles(); const items = []; function returnFourCards(i) { return ( <Grid container item xs={12} spacing={3} className={classes.gridContainer} > <React.Fragment> <Grid item xs={3}> <Link to={"/updategroup/" + props.groupList[i].Number} className={classes.linkStyle} > <GroupsCard AD_groups={props.groupList[i].AD_groups} Team={props.groupList[i].Number} Owner={props.groupList[i].Owner} Email={props.groupList[i].Email} Name={props.groupList[i].Name} /> </Link> </Grid> {console.log(i++)} <Grid item xs={3}> <Link to={"/updategroup/" + props.groupList[i].Number} className={classes.linkStyle} > <GroupsCard AD_groups={props.groupList[i].AD_groups} Team={props.groupList[i].Number} Owner={props.groupList[i].Owner} Email={props.groupList[i].Email} Name={props.groupList[i].Name} /> </Link> </Grid> {console.log(i++)} <Grid item xs={3}> <Link to={"/updategroup/" + props.groupList[i].Number} className={classes.linkStyle} > <GroupsCard AD_groups={props.groupList[i].AD_groups} Team={props.groupList[i].Number} Owner={props.groupList[i].Owner} Email={props.groupList[i].Email} Name={props.groupList[i].Name} /> </Link> </Grid> {console.log(i++)} <Grid item xs={3}> <Link to={"/updategroup/" + props.groupList[i].Number} className={classes.linkStyle} > <GroupsCard AD_groups={props.groupList[i].AD_groups} Team={props.groupList[i].Number} Owner={props.groupList[i].Owner} Email={props.groupList[i].Email} Name={props.groupList[i].Name} /> </Link> </Grid> {console.log(i++)} </React.Fragment> </Grid> ); } function returnThreeCards(i) { return ( <Grid container item xs={12} spacing={3}> <React.Fragment> <Grid item xs={3}> <Link to={"/updategroup/" + props.groupList[i].Number} className={classes.linkStyle} > <GroupsCard AD_groups={props.groupList[i].AD_groups} Team={props.groupList[i].Number} Owner={props.groupList[i].Owner} Email={props.groupList[i].Email} Name={props.groupList[i].Name} /> </Link> </Grid> {console.log(i++)} <Grid item xs={3}> <Link to={"/updategroup/" + props.groupList[i].Number} className={classes.linkStyle} > <GroupsCard AD_groups={props.groupList[i].AD_groups} Team={props.groupList[i].Number} Owner={props.groupList[i].Owner} Email={props.groupList[i].Email} Name={props.groupList[i].Name} /> </Link> </Grid> {console.log(i++)} <Grid item xs={3}> <Link to={"/updategroup/" + props.groupList[i].Number} className={classes.linkStyle} > <GroupsCard AD_groups={props.groupList[i].AD_groups} Team={props.groupList[i].Number} Owner={props.groupList[i].Owner} Email={props.groupList[i].Email} Name={props.groupList[i].Name} /> </Link> </Grid> {console.log(i++)} </React.Fragment> </Grid> ); } function returnTwoCards(i) { return ( <Grid container item xs={12} spacing={3}> <React.Fragment> <Grid item xs={3}> <Link to={"/updategroup/" + props.groupList[i].Number} className={classes.linkStyle} > <GroupsCard AD_groups={props.groupList[i].AD_groups} Team={props.groupList[i].Number} Owner={props.groupList[i].Owner} Email={props.groupList[i].Email} Name={props.groupList[i].Name} /> </Link> </Grid> {console.log(i++)} <Grid item xs={3}> <Link to={"/updategroup/" + props.groupList[i].Number} className={classes.linkStyle} > <GroupsCard AD_groups={props.groupList[i].AD_groups} Team={props.groupList[i].Number} Owner={props.groupList[i].Owner} Email={props.groupList[i].Email} Name={props.groupList[i].Name} /> </Link> </Grid> {console.log(i++)} </React.Fragment> </Grid> ); } function returnOneCard(i) { return ( <Grid container item xs={12} spacing={3}> <React.Fragment> <Grid item xs={3}> <Link to={"/updategroup/" + props.groupList[i].Number} className={classes.linkStyle} > <GroupsCard AD_groups={props.groupList[i].AD_groups} Team={props.groupList[i].Number} Owner={props.groupList[i].Owner} Email={props.groupList[i].Email} Name={props.groupList[i].Name} /> </Link> </Grid> {console.log(i++)} </React.Fragment> </Grid> ); } for (let i = 0; i < props.groupList.length; ) { let cardContent; if (props.groupList.length - i >= 4) { cardContent = returnFourCards(i); i += 4; } else if (props.groupList.length - i === 3) { cardContent = returnThreeCards(i); i += 3; } else if (props.groupList.length - i === 2) { cardContent = returnTwoCards(i); i += 2; } else if (props.groupList.length - i === 1) { cardContent = returnOneCard(i); i += 1; } items.push(cardContent); } return ( <div className={classes.root}> <Grid container spacing={1}> {items.map((item) => item)} </Grid> </div> ); }

This code is working but It is not optimised.此代码有效,但未优化。 I want to learn an efficient way to write this.我想学习一种有效的方法来写这个。

you can shorten this code by a lot by using map function and ternary operators to decide the styling if that is your goal.如果这是您的目标,您可以使用 map function 和三元运算符来决定样式,从而大大缩短此代码。

export default function NestedGrid(props) {
  const classes = useStyles();

  const { groupList } = props;

  return (
    <div className={classes.root}>
      <Grid container spacing={1}>
        {groupList.map((item, index) => {
          return (
            <Grid container item xs={12} spacing={3}>
              <Grid item xs={3}>
                <Link
                  to={"/updategroup/" + item[index].Number}
                  className={classes.linkStyle}
                >
                  <GroupsCard
                    AD_groups={item[index].AD_groups}
                    Team={item[index].Number}
                    Owner={item[index].Owner}
                    Email={item[index].Email}
                    Name={item[index].Name}
                  />
                </Link>
              </Grid>
            </Grid>
          );
        })}
      </Grid>
    </div>
  );
}

Now regarding any changes you want to implement I would go with inline styling if it is not too complex, if it is would create classnames based on amount of cards and move the styling to a separate css/scss file.现在关于您想要实现的任何更改,如果它不是太复杂,我会使用内联样式 go,如果它会根据卡片数量创建类名并将样式移动到单独的 css/scss 文件。

If you have any questions feel free to comment them down below and will edit my answer accordingly.如果您有任何问题,请随时在下面发表评论,并将相应地编辑我的答案。

UPDATE:更新:

If you want to display more than 1 card in a row (for example 4) all you have to do it remove the first Grid tag that is xs={12} as it means it will take the entire space and put it outside the map function.如果您想连续显示多张卡片(例如 4 张),您只需删除第一个 Grid 标签 xs={12} 因为这意味着它将占用整个空间并将其放在 map 之外function。

now if you want to have more control and decide what will render how before the return line you can write conditions and multiple returns instead but the best way will be to not use the Grid tag and have a regular div which you can fully style inline or in an external css/scss file.现在,如果您想拥有更多控制权并决定在返回行之前如何渲染,您可以编写条件和多个返回,但最好的方法是不使用 Grid 标签并拥有一个常规 div ,您可以完全设置内联样式或在外部 css/scss 文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM