React: Render new row every 4th column

前端 未结 7 1059
野的像风
野的像风 2020-12-30 03:55

I\'m using react and I want to render a new row every 4th column.

My code:

function Product(props) {
  const content = prop         


        
7条回答
  •  囚心锁ツ
    2020-12-30 04:15

    You want to wrap new row for each 4 columns, right?

    Try this

    render() {
    		const products = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    		let rowContents = [];
    		const contents = products.reduce((acc, p, i) => {
    			rowContents.push(
    Col {p}
    ); if (i % 4 === 3) { acc.push(
    {rowContents}
    ); rowContents = []; } return acc; },[]) contents.push(
    {rowContents}
    ); return (
    {contents}
    ) }

提交回复
热议问题