My RN 0.62.2 app renders uploaded images on screen in 2 columns with each image in a square. When uploading 2 images, the following render code works fine:
return (
<Grid style={{position:"absolute", paddingTop:0,paddingLeft:0}}> //<<==wrap in Grid
<Row style={{paddingTop:0}}> //<<==2 images shown in one row
<Col style={{position:"absolute", marginTop:0}}> //<<==2 columns
{displayImg(pics[0].path, screen_width*half, screen_width*half, 0, screen_width, pics[0].height*(screen_width/pics[0].width))} //<<==method to display one image
</Col>
<Col style={{position:"absolute", left:Math.ceil((screen_width-20)/2), paddingTop:0}}>
{displayImg(pics[1].path, screen_width*half, screen_width*half, 1, screen_width, pics[1].height*(screen_width/pics[1].width))}
</Col>
</Row>
</Grid>
);
The displayImg is the method displaying each individual image. Now I would like to use the same code to process between 2 and 4 images. It seems that it is natural to use array to hold the repeating render code. Here is the code after refactoring:
var rows = [];
for (let i=0;i<len;i+2) { //<<==added for loop to push each row into `rows`
if (pics[i+1]) {
rows.push(
<Row style={{paddingTop:0}}>
<Col style={{position:"absolute", marginTop:0}}>
{displayImg(pics[i].path, screen_width*half, screen_width*half, i, screen_width, pics[i].height*(screen_width/pics[i].width))}
</Col>
<Col style={{position:"absolute", left:Math.ceil((screen_width-20)/2), paddingTop:0}}>
{displayImg(pics[i+1].path, screen_width*half, screen_width*half, i+1, screen_width, pics[i+1].height*(screen_width/pics[i+1].width))}
</Col>
</Row>
);
} else {
rows.push(
<Row style={{paddingTop:0}}>
<Col style={{position:"absolute", marginTop:0}}>
{displayImg(pics[i].path, screen_width*half, screen_width*half, i, screen_width, pics[i].height*(screen_width/pics[i].width))}
</Col>
<Col style={{position:"absolute", left:Math.ceil((screen_width-20)/2), paddingTop:0}}>
</Col>
</Row>
);
};
};
return(
<Grid style={{position:"absolute", paddingTop:0,paddingLeft:0}}> //<<==wrap in grid
{rows}
</Grid>
);
Tested with uploading 2 images, there is nothing returned and the app hung. What's wrong with the refactoring?