How to concatenate JSX components in React Native
For example, I wanna show a list of names. So I wanted to do something like this:
var returnValue;
for (eachName of _names) {
returnValue += (
<TouchableHighlight
onPress={() => this._onPressButton}>
<Text>
{eachName}
</Text>
</TouchableHighlight>);
}
return returnValue;
However, that is not valid. Which brings me to my question: How do I concatenate a dynamic amount of JSX components in React Native.
Figures I figure it out soon as I ask stackoverflow. The code needs to be put into an array:
var returnValue = [];
for (var i = 0; i < _names.length; i++) {
returnValue.push(
<TouchableHighlight onPress={() => this._onPressButton}>
<Text>
{_names[i]}
</Text>
</TouchableHighlight>);
}
return returnValue;
There is also more information here: http://facebook.github.io/react/docs/multiple-components.html#dynamic-children
Maybe a more elegant way:
return <View>
{_names.map((eachName) => {
return (
<TouchableHighlight onPress={() => this._onPressButton}>
<Text>
{eachName}
</Text>
</TouchableHighlight>
);
})}
</View>