Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass data to SectionList?

Tags:

react-native

I am really confused as I don't understand how to pass an array to the SectionList. When I try this one everything is ok

 <SectionList
     renderItem={({item}) => <View><Text> title={item.title}</Text></View>}
      renderSectionHeader={({section}) => 
          <View><Text> title={section.key}</Text></View>}
          sections={[
               {data: [{title:123},{title:"aaa"}], key: "aaa"},
               data: [{title:333},{title:"bbbb"}], key: "bbb"},
               {data: [{title:123},{title:"aaa"}], key: "ccc"},
          ]}
          />

But if I try to use a function (as I understand it is totally the same), I got an error "props.sections.reduce is not a function". How to pass an array with function to the SectionList? Thanks

 sections={()=>[
        {data: [{title:123},{title:"aaa"}], key: "aaa"},
         data: [{title:333},{title:"bbbb"}], key: "bbb"},
         {data: [{title:123},{title:"aaa"}], key: "ccc"},
        ]}
  />
like image 454
SERG Avatar asked May 26 '17 06:05

SERG


People also ask

What is the difference between FlatList and SectionList?

SectionList s are like FlatList s, but they can have section headers to separate groups of rows. SectionList s render each item in their input sections using the renderSectionHeader and renderItem prop. Each item in sections should be an object with a unique id (the key), and an array data of row data.

Which interface do FlatList and SectionList extend from?

We use Sectionlist and Flatlist in our react native project and we use websocket to receive/update data.


2 Answers

Sections props require an array not a function. To use a function in sections props, this function must return an array. The array should contain a string key, and a data object. You can use many parameter in this object, for must information you can consult this page

example:

export default class Example extends React.Component {

  selectionList = () => {
    return([
      {data: [{title:123},{title:"aaa"}], key: "aaa"},
      {data: [{title:333},{title:"bbbb"}], key: "bbb"},
      {data: [{title:123},{title:"aaa"}], key: "ccc"},
    ])
  }

  render () {
    return (
      <View>
        <SectionList
          renderItem={({item}) => <View><Text> title={item.title}</Text></View>}
          renderSectionHeader={({section}) => <View><Text> title={section.key}</Text></View>}
          sections={this.selectionList()}
        />
      </View>
    )
  }
}
like image 166
Alexandre Teixeira Avatar answered Oct 06 '22 17:10

Alexandre Teixeira


what's wrong is the first you try to pass a function to section props, not a value. to get value return from a function, you need to execute that function. Example :

sections={(()=>[
         {data: [{title:123},{title:"aaa"}], key: "aaa"},
          {data: [{title:333},{title:"bbbb"}], key: "bbb"},
          {data: [{title:123},{title:"aaa"}], key: "ccc"},
          ]) () <<< execute function 
          }
         />
like image 32
Hoang Hoang Avatar answered Oct 06 '22 15:10

Hoang Hoang