Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native 'Unordered'-style List

Tags:

react-native

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 Views (one to contain the 'bullet' & the other the list item text) or is their an easier, less cumbersome way?

like image 496
Steed-Asprey Avatar asked Aug 23 '16 20:08

Steed-Asprey


People also ask

How do you add a bullet point in React Native?

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.

What is difference between FlatList and ListView React Native?

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!


1 Answers

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>   ); } 
like image 117
Michael Helvey Avatar answered Sep 20 '22 01:09

Michael Helvey