0

I have a array that contains 5 elements:

[
  {
    "id": 1,
    "title": "Title 1",
    "description": "descriptions"
  },
  {
    "id": 2,
    "title": "Title 1",
    "description": "descriptions"
  },
  {
    "id": 3,
    "title": "Title 1",
    "description": "descriptions"
  },
  {
    "id": 4,
    "title": "Title 1",
    "description": "descriptions"
  },
  {
    "id": 5,
    "title": "Title 1",
    "description": "descriptions"
  }
]

And I want them to show up as follows:

<Item>
  <Text style={styles.cardText}>Title 1</Text>
  <Text style={styles.cardText} numberOfLines={2}>Description 1</Text>
</Item>
<Grid>
  <Col>
    <Item>
      <Text style={styles.cardText}>Title 2</Text>
      <Text style={styles.cardText} numberOfLines={2}>Description 2</Text>
    </Item>
  </Col>
  <Col>
    <Item>
      <Text style={styles.cardText}>Title 3</Text>
      <Text style={styles.cardText} numberOfLines={2}>Description 3</Text>
    </Item>
  </Col>
</Grid>
<Grid>
  <Col>
    <Item>
      <Text style={styles.cardText}>Title 4</Text>
      <Text style={styles.cardText} numberOfLines={2}>Description 4</Text>
    </Item>
  </Col>
  <Col>
    <Item>
      <Text style={styles.cardText}>Title 5</Text>
      <Text style={styles.cardText} numberOfLines={2}>Description 5</Text>
    </Item>
  </Col>
</Grid>

How to display the above as in React Native?

I tried to use datas.map function but could not. I tried to adding string but I thought that was not the solution.

Is there a solution for it?

Thank all.

0

2 Answers 2

1

You can chunk the array into 2 parts for indexes other than 0.

constructor() {
    super();
    this.state = {
        data: [
            {
                "id": 1,
                "title": "Title 1",
                "description": "descriptions"
            },
            {
                "id": 2,
                "title": "Title 1",
                "description": "descriptions"
            },
            {
                "id": 3,
                "title": "Title 1",
                "description": "descriptions"
            },
            {
                "id": 4,
                "title": "Title 1",
                "description": "descriptions"
            },
            {
                "id": 5,
                "title": "Title 1",
                "description": "descriptions"
            }
        ],
        chunk: this.chunk
    }
} 

Method for chunking array:

chunk(array, chunkSize) {
    return [].concat.apply([],
        array.map(function (elem, i) {
            return i % chunkSize ? [] : [array.slice(i, i + chunkSize)];
        })
    );
};

Rendering the items:

render() {
    let { data, chunk } = this.state;
    let dataForChunk = [...data];
    dataForChunk.splice(0, 1); //removes 1st index of array
    let chunkedArr = chunk(dataForChunk, 2);

    return (<View style={{flex: 1}}>
        <Item>
            <Text style={styles.cardText}> {data[0].title} </Text>
            <Text style={styles.cardText} numberOfLines={2} > {data[0].description} </Text>
        </Item>
        {chunkedArr.map((arr, index) => {
            return(
                <Grid>
                    {arr.map(item => {
                        return(
                            <Col>
                                <Item>
                                    <Text style={styles.cardText} >{item.id}</Text>
                                    <Text style={styles.cardText} numberOfLines={2}>{item.description}</Text>
                                </Item>
                            </Col>
                        );
                    })}
                </Grid>
            )
        })}
    </View>)
}
Sign up to request clarification or add additional context in comments.

1 Comment

I am used it. Thank you very must!
0

You can use FlatList instead.

Considering example

const row = [{key: 'a'}, {key: 'b'}]

FlatList Usage:

<FlatList
        data={row}
        renderItem={(item) => this._renderItem(item)}
        keyExtractor={extractKey}
        extraData={row}
        />

Rendering View inside function:

    _renderItem = ({item}) => {
    return <Text>{item.key}</Text>
   }

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.