0

How can I style 24 elements in an array with a table of 4 columns and 6 rows?

The array has 24 elements like this:

const words = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x" ]

and I map them over in render method:

<View>
  {words.map((word, id) => (
    <View key={id}><Text>{word} </Text></View>
  ))}
</View>
1
  • First you'll want to chunk your array into sub-arrays of 4 each, so it looks like [["a", "b", "c", "d"],["e", "f", "g", "h"]... -- you can use lodash.chunk for this. Commented Jun 13, 2019 at 13:59

1 Answer 1

1

You can use some simple flex properties:

const styles = StyleSheet.create({
  table: {
    flexWrap: 'wrap',
    flexDirection: 'row'
  },
  cell: {
    flexBasis: '25%',
    flex: 1,
    borderWidth: 1
  }
});

And then:

<View style={styles.table}>
  {words.map((word, id) => (
    <View style={styles.cell} key={id}><Text>{word} </Text></View>
  ))}
</View>

https://snack.expo.io/@morkadosh/mad-salsa

Sign up to request clarification or add additional context in comments.

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.