Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I dynamically create text in React Native using an array as the text source?

I would like to create 8 lines of text based on a pre-made array of text. For example

const someInfo = ["Mobile Phones", "Restaurants", "Tv Channels", "Music", "Health", "Wifi", "Real Estate", "Meetups"];

from there just a simple

export default class User extends Component{
    render(){
        return(
            <View style={styles.mainBoxes}>
                <Text style={styles.mainBoxesText}>{textfromArrayHere}
                </Text>
            </View>

        );
    }
}

how would I go about looping through that array and dynamically insert the text?

like image 418
VK1 Avatar asked Apr 20 '26 21:04

VK1


1 Answers

consider your array is someInfo, do it the simple way:

<View style={styles.mainBoxes}>
  {someInfo.map(info => <Text>{info}</Text>)}
</View>

Remember: I just give example => You put your own way for styling

Cooler way => using official FlatList with renderItem props also the info => <Text>{info}</Text): https://facebook.github.io/react-native/docs/flatlist.html

like image 199
Khoa Avatar answered Apr 22 '26 09:04

Khoa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!