Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native - SectionList numColumns support

FlatList has numColumns support. How to set numColumns with SectionList?

Github issue: SectionList renderItem multi item support #13192

like image 500
Pir Shukarullah Shah Avatar asked Dec 15 '17 13:12

Pir Shukarullah Shah


4 Answers

Here is my solution to numColumns for SectionList. If you have better let me know please.

class Example extends Component {
  static propTypes = {
    numColumns: PropTypes.number
  };

  static defaultProps = {
    numColumns: 2
  };

  _renderSection = data => <Section {...data} />;

  _renderItem = ({ section, index }) => {
    const { numColumns } = this.props;

    if (index % numColumns !== 0) return null;

    const items = [];

    for (let i = index; i < index + numColumns; i++) {
      if (i >= section.data.length) {
        break;
      }

      items.push(<Item item={section.data[i]} />);
    }

    return (
      <View
        style={{
          flexDirection: "row",
          justifyContent: "space-between"
        }}
      >
        {items}
      </View>
    );
  };

  render() {
    return (
      <SectionList
        sections={dumyData}
        style={styles.container}
        renderItem={this._renderItem}
        renderSectionHeader={this._renderSection}
      />
    );
  }
}
like image 148
Pir Shukarullah Shah Avatar answered Sep 20 '22 02:09

Pir Shukarullah Shah


Digging this issue up, I came with a solution similar to Pir Shukarullah Shah 's.

I'm using FlatList instead of my regular item, taking into account only the first item in <SectionList/>'s renderItem method.

 _renderList = ({ section, index }) => {
    if (index !== 0) return null;

    return (
      <FlatList numColumns={columns}
        columnWrapperStyle={styles.container}
        data={section.data}
        renderItem={this._renderItem}
        keyExtractor={keyExtractor}
      />
    )
  }



...
      <SectionList
        renderItem={this._renderList}
        renderSectionHeader={this._renderSectionHeader}
        sections={itemList}
        keyExtractor={keyExtractor}
      />
like image 44
kriskate Avatar answered Sep 20 '22 02:09

kriskate


It is possible to use FlatList with numColumns prop as the renderItem of SectionList.

const data = [ //Notice [[...]] instead of [...] as in the RN docs
    {data: [[...]], title: ...},
    {data: [[...]], title: ...},
    {data: [[...]], title: ...},
]

render () {
    return (
        <SectionList
            renderItem={this._renderSectionListItem}
            renderSectionHeader={this._renderSectionHeader}
            sections={data}
        />
    )
}

renderSectionListItem = ({item}) => {
    return (
        <FlatList
            data={item}
            numColumns={3}
            renderItem={this.renderItem}
        />
    )
}
like image 45
Justin Lok Avatar answered Sep 19 '22 02:09

Justin Lok


I found there is a simple solution. Please try adding the following property to the

contentContainerStyle={{
    flexDirection  : 'row',
    justifyContent : 'flex-start',
    alignItems     : 'flex-start',
    flexWrap       : 'wrap'
}}

Besides, set and render the Section Header with the Width equal to the SectionList width. Otherwise, the list items will be displayed following the Section Header in row direction.

like image 39
Fong Gabriel Avatar answered Sep 19 '22 02:09

Fong Gabriel