How would one go about creating a list in React Native that looked similar to an unordered list (<ul>
) in HTML? Does it require a flex based layout with two View
s (one to contain the 'bullet' & the other the list item text) or is their an easier, less cumbersome way?
Introduction. Something you will commonly find yourself needing to do in React Native is create a bulleted list, but it isn't as straightforward as you might think. Unlike in HTML and CSS, there is no unordered list tag that will add the bullets for us - instead we need to use unicode to add them.
FlatList - More performant compared to ListView. ListView rendering can get slow once the number of items grows larger. FlatList significantly improves memory usage and efficiency (especially for large or complex lists) while also significantly simplifying the props — no more dataSource necessary!
Potentially, the easiest way would be just to use a unicode character for the bullet. That way you don't have wrap a bunch of components together.
For example, the following component that uses a ListView
(see the renderRow
function for the bullet):
class TestProject extends React.Component { constructor() { super(); this.state = { dataSource: new ListView.DataSource({ rowHasChanged: (row1, row2) => row1 !== row2, }).cloneWithRows(['string1', 'string2', 'string3']), }; } renderRow(data) { return ( <Text>{`\u2022 ${data}`}</Text> ); } render() { return ( <ListView style={{margin: 40}} dataSource={this.state.dataSource} renderRow={this.renderRow} /> ); } }
If you need to keep the text from wrapping around the bullet, you will actually need to use multiple components, as suggested in the question. For example:
renderRow(data) { return ( <View style={{flexDirection: 'row'}}> <Text>{'\u2022'}</Text> <Text style={{flex: 1, paddingLeft: 5}}>{data}</Text> </View> ); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With