react-native – React Native – SectionList numColumns支持

前端之家收集整理的这篇文章主要介绍了react-native – React Native – SectionList numColumns支持前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
FlatList有numColumns支持.如何用 SectionList设置numColumns?

Github问题:SectionList renderItem multi item support #13192

这是我对SectionList的numColumns的解决方案.如果你有更好的请告诉我.
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}
      />
    );
  }
}

猜你在找的React相关文章