How do I render section headers with ListView in react native?

MonkeyBonkey

I'm looking at the documentation and see that I need to implement renderSectionHeader but what else do I need to implement and how do I prepare the data to encode the section information? I don't see anything in the facebook documentation that explains that and I'm getting an error _this.getSectionId is not a function

  ...
  renderSectionHeader = (sectionData, sectionID) => {
    return (
      <Divider styleName="section-header">
        <Caption>DO SOMETHING WITH sectionData?</Caption>
      </Divider>
    );
  };
  ...
        <ListView
          data = { results }
          autoHideHeader = { true }
          loading = { isLoading }
          renderHeader = { this.renderHeader }
          onRefresh = { data.refetch }
          renderSectionHeader={ this.renderSectionHeader }
          renderRow={ this.renderRow }
        />
Joshua Pinter

Working Example

Here's a working sample that I've modified from this excellent post.

First of all, your dataBlob:

var dataBlob = {
  'sectionID1' : { name: 'section1' },
  'sectionID1:rowID1' : { name: 'section1row1' },
  'sectionID1:rowID2' : { name: 'section1row2' },
  'sectionID2' : { name: 'section2' },
  'sectionID2:rowID1' : { name: 'section2row1' },
  'sectionID2:rowID2' : { name: 'section2row2' },
}

Whatever data that you have will have to be manipulated. It doesn't have to look exactly like this but for this example, this is what we're going to tell the ListView to expect.

Now we need to tell ListView how to extract the sections and the rows from this dataBlob.

var getSectionData = (dataBlob, sectionID) => {
  return dataBlob[sectionID];
}

var getRowData = (dataBlob, sectionID, rowID) => {
  return dataBlob[sectionID + ':' + rowID];
}

You can tell by the two methods above that you can have a different structure to your dataBlob, you just need to update these two methods to let it know how to access it.

Now we'll create a dataSource that uses sections.

this.ds = new ListView.DataSource({
  getSectionData: getSectionData,
  getRowData: getRowData,
  rowHasChanged: (r1, r2) => r1 !== r2,
  sectionHeaderHasChanged: (s1, s2) => s1 !== s2
})

This part seems redundant to me but is required. You have to list out the section ids and the row ids in separate arrays.

var sectionIDs = [ 'sectionID1', 'sectionID2' ];

var rowIDs = [ [ 'rowID1', 'rowID2' ], [ 'rowID1', 'rowID2' ] ];

Now you need to update the state with the dataSource:

this.state = {
  dataSource: this.ds.cloneWithRowsAndSections( dataBlob, sectionIDs, rowIDs )
}

And, finally, you need to display it in your ListView.

<ListView
  dataSource={this.state.dataSource}
  renderRow={(rowData, sectionID, rowID) => {
    return <Text>{rowData.name}</Text>
  }} 
  renderSectionHeader={ (sectionData, sectionID) => {
    return <Text>{sectionData.name}</Text>
  }}
/>

That'll get you started. I hope that helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I render a function which contain a view in react native?

From Dev

How do I render a component when a button is clicked in React Native

From Dev

React Native: How can I focus on ListView?

From Dev

React native: ListView doesn't render a row

From Dev

How to make Android NOT count ListView's section headers?

From Dev

How to make Android NOT count ListView's section headers?

From Dev

how to render objects in react native?

From Dev

React-native Airbnb-Maps: How do I render a button inside the airbnb maps pop-up box?

From Dev

How do I enable multidex for react native?

From Dev

How do I properly debug in React Native?

From Dev

React Native - How to prepend AND append data in ListView without full re-render

From Dev

how I can do a section in other section?

From Dev

In React Native (and Ignite), how do I create a ListView (inside a tab) that navigates to another container when a list item is clicked?

From Dev

How do I SAFELY render Markdown from a React component?

From Dev

How do I render Markdown from a React component?

From Dev

How do I render a div and script tags in React?

From Dev

How do I render components within a state in react?

From Dev

How do I render json in my react component?

From Dev

How do I render a Material UI icon in React?

From Dev

Custom WPF ListView with Style (using DataTemplate) - how do I add headers?

From Dev

Custom WPF ListView with Style (using DataTemplate) - how do I add headers?

From Dev

How do render react in the browser

From Dev

How can I enable/disable section headers in UICollectionView programmatically?

From Dev

How can I style the zsh completion section headers?

From Dev

How to render component with ajax in react native?

From Dev

How to render checkbox on the <Image> (React Native)

From Dev

How to render items in two columns in react native?

From Dev

how to set state in render react native

From Dev

How to render components using function in React Native

Related Related

  1. 1

    How do I render a function which contain a view in react native?

  2. 2

    How do I render a component when a button is clicked in React Native

  3. 3

    React Native: How can I focus on ListView?

  4. 4

    React native: ListView doesn't render a row

  5. 5

    How to make Android NOT count ListView's section headers?

  6. 6

    How to make Android NOT count ListView's section headers?

  7. 7

    how to render objects in react native?

  8. 8

    React-native Airbnb-Maps: How do I render a button inside the airbnb maps pop-up box?

  9. 9

    How do I enable multidex for react native?

  10. 10

    How do I properly debug in React Native?

  11. 11

    React Native - How to prepend AND append data in ListView without full re-render

  12. 12

    how I can do a section in other section?

  13. 13

    In React Native (and Ignite), how do I create a ListView (inside a tab) that navigates to another container when a list item is clicked?

  14. 14

    How do I SAFELY render Markdown from a React component?

  15. 15

    How do I render Markdown from a React component?

  16. 16

    How do I render a div and script tags in React?

  17. 17

    How do I render components within a state in react?

  18. 18

    How do I render json in my react component?

  19. 19

    How do I render a Material UI icon in React?

  20. 20

    Custom WPF ListView with Style (using DataTemplate) - how do I add headers?

  21. 21

    Custom WPF ListView with Style (using DataTemplate) - how do I add headers?

  22. 22

    How do render react in the browser

  23. 23

    How can I enable/disable section headers in UICollectionView programmatically?

  24. 24

    How can I style the zsh completion section headers?

  25. 25

    How to render component with ajax in react native?

  26. 26

    How to render checkbox on the <Image> (React Native)

  27. 27

    How to render items in two columns in react native?

  28. 28

    how to set state in render react native

  29. 29

    How to render components using function in React Native

HotTag

Archive