React Native: how to properly pass renderItem items to FlatList, so they can be rendered in another component?

889 views Asked by At

For testing how a flatlist works, I'm trying to display items in it using another component for the "renderItem" function.

Here's my code that renders the FlatList:

export default function HomeBody() {
    const data = [];

    let movie1: MediaDetails = {
        title: 'Movie1',
        image: '../../assets/TestImage.png'
    };
    let movie2: MediaDetails = {
        title: 'Movie1',
        image: '../../assets/TestImage.png'
    };
    let movie3: MediaDetails = {
        title: 'Movie1',
        image: '../../assets/TestImage.png'
    };
    let myQueue: SwimlaneItem = {
        title: 'My queue',
        media: [
            movie1,
            movie2,
            movie3,
        ]
    }

    let movie4: MediaDetails = {
        title: 'Movie4',
        image: '../../assets/TestImage.png'
    };
    let movie5: MediaDetails = {
        title: 'Movie5',
        image: '../../assets/TestImage.png'
    };
    let movie6: MediaDetails = {
        title: 'Movie6',
        image: '../../assets/TestImage.png'
    };
    let upcomingQueue: SwimlaneItem = {
        title: 'My queue',
        media: [
            movie4,
            movie5,
            movie6,
        ]
    }

    data.push(myQueue);
    data.push(upcomingQueue);

    return(
        <View style={styles.container}>
            <FlatList 
                style={styles.flatlist}
                data={data}
                renderItem={({ item }: any) => 
                  <Swimlane swimlaneItem={item} />
                }
            />
        </View>
    )
} 

And here's my component "per item" or "Swimlane":

export default function Swimlane({ swimlaneItem }: any) {
    return (
        <View>
            <Text>{swimlaneItem}</Text>
            {/* <FlatList 
                horizontal
                data={swimLaneItem.media}
                renderItem={({ media }: any) => 
                    <>
                        <Text>{media.title}</Text>
                        <Image source={media.image} />
                    </>
                }
            /> */}
        </View>
    )
}

So what I want is for the flatlist to display 2 "lanes" or items, these are "myQueue" and "upcomingQueue". Each "lane" will have a title and a media array, containing items which have a "title" and an "image". So in my "Swimlane" component I want to do "swimlaneItem.title" for the text and have another flatlist to render all the "media" objects, which will in turn have a "title" and "media" property.

How can I achieve this? Right now the following error is thrown:

"Objects are not valid as a React child (found: object with keys {title, media}). If you meant to render a collection of children, use an array instead"
1

There are 1 answers

1
Walter Shub On BEST ANSWER

the reason for the error is that your passing the entire render item object into a component instead of a string which is what Text expects

const RenderItem = ({item}) => 
   <View>
    <Text>{item.someKEyWhichIsAstring}<Text/>
    <Image
      style={styles.marker}
      source={item.someKeyWhichIsAnValidUrlString}/>
  <View/>
```

if you're looking have multiple sections you should use a SectionList
https://reactnative.dev/docs/sectionlist
its inherits from flautist (which inherits from ScrollView) and has and API for multiple sections