How do you deal with conditional within a loop in reactjsx where I want to create a <div> and closing </div> every so often so I can create rows
Right now I have something like this:
renderContent = () => {
const { classes, product } = this.props;
if (!product) {
return (
<>
<Content />
<Space width="10px" />
<Content />
<Space width="10px" />
<Content />
<Space width="10px" />
<Content />
<Space width="10px" />
<Content />
</>
);
}
const sizeLength = product.length;
const content = [];
for (let i = 0; i < sizeLength; i++) {
if (i === 0 || i % 5) content.push(<div>)
content.push(
<Fragment key={`${i}`}>
<Content />
<Space width="10px" />
</Fragment>,
);
if (i === 4 || i % 4 ) content.push(</div>)
}
return content;
}
So the code renders 5 <Content /> if product is null. This is all in one row.
What I'm trying to accomplish is to have something like this:
<div>
<Content />
<Space width="10px" />
<Content />
<Space width="10px" />
<Content />
<Space width="10px" />
<Content />
<Space width="10px" />
<Content />
</div>
A wrapping every 5 loop, so if there's a 6th, it would be someting like this:
<div>
<Content />
<Space width="10px" />
<Content />
<Space width="10px" />
<Content />
<Space width="10px" />
<Content />
<Space width="10px" />
<Content />
</div>
<div>
<Content />
</div>
But the code I have is breaking and doesn't work. Is there something I'm missing? I'm not sure if the content.push is correct, perhaps there's a better way of dealing with this?