Search code examples
javascriptreact-nativereact-native-flatlistreact-native-sectionlist

SectionList data variable change to something like items in react native


How can i change the scheme for data to something like items or agents in our array that we providing to sectionList. section list accepts array like this : const DATA = [ { title: 'Main dishes', data: ['Pizza', 'Burger', 'Risotto'], }, { title: 'Sides', data: ['French Fries', 'Onion Rings', 'Fried Shrimps'], }, { title: 'Drinks', data: ['Water', 'Coke', 'Beer'], }, { title: 'Desserts', data: ['Cheese Cake', 'Ice Cream'], }, ]; is it possible to change the data to agent const DATA = [ { title: 'Main dishes', agent: ['Pizza', 'Burger', 'Risotto'], }, { title: 'Sides', agent: ['French Fries', 'Onion Rings', 'Fried Shrimps'], }, { title: 'Drinks', agent: ['Water', 'Coke', 'Beer'], }, { title: 'Desserts', agent: ['Cheese Cake', 'Ice Cream'], }, ];


Solution

  • I solved this by passing the whole section to the function and then got the item from section based on the index

    const DATA = [{
        title: 'Main dishes',
        agent: ['Pizza', 'Burger', 'Risotto'],
        data: ['Pizza', 'Burger', 'Risotto']
    }, {
        title: 'Sides',
        agent: ['French Fries', 'Onion Rings', 'Fried Shrimps'],
        data: ['French Fries', 'Onion Rings', 'Fried Shrimps'],
    }, { title: 'Drinks', agent: ['Water', 'Coke', 'Beer'], }, {
        title: 'Desserts',
        agent: ['Cheese Cake', 'Ice Cream'],
        data: ['Cheese Cake', 'Ice Cream'],
    },];
    
    const renderItem=({ section, item, index })=>{
    return <RenderItemComponent data={section.agent[index]} key={index} />
    }
    
    <SectionList
      sections={DATA}
      renderItem={renderItem} 
      renderSectionHeader={renderSectionHeader}
    />