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"},
]}
/>
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.
We use Sectionlist and Flatlist in our react native project and we use websocket to receive/update data.
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>
)
}
}
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
}
/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With