0

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?

2 Answers 2

1

Your variable rows is array, so you have to map on it to display.

return(
  <Grid style={{position:"absolute", paddingTop:0,paddingLeft:0}}>  //<<==wrap in grid
      {rows.map((item) => (
        item
      ))}
  </Grid>
);
Sign up to request clarification or add additional context in comments.

1 Comment

the map didn't help with the same problem.
0

Solved the problem by doing:

           return (                   
                <Grid style={{position:"absolute", paddingTop:0,paddingLeft:0}}>
                    <Row style={styles.row}>   
                        {pics.map((item, index) => {
                           return (displayImg(item.path, screen_width*half, screen_width*half, index,  screen_width, item.height*(screen_width/item.width)))
                        })}                 
                    </Row>
                </Grid>
            );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.