Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I handle heterogeneous data in SectionList


How do I handle heterogeneous data in SectionList


Handling heterogeneous data in a React Native `SectionList` involves a few key steps:

1. Structuring the Data:
- Organize your data into sections, where each section has a `title` and a `data` array.
- The `data` array can contain items of different types or structures.

Example data structure:

javascript
const sections = [
  {
    title: 'Section 1',
    data: [
      { id: 1, type: 'text', content: 'Item 1' },
      { id: 2, type: 'image', imageUrl: 'https://example.com/image1.jpg' },
      { id: 3, type: 'video', videoUrl: 'https://example.com/video1.mp4' },
    ],
  },
  {
    title: 'Section 2',
    data: [
      { id: 4, type: 'text', content: 'Item 4' },
      { id: 5, type: 'image', imageUrl: 'https://example.com/image2.jpg' },
    ],
  },
];

2. Rendering Heterogeneous Items:
- Use the `renderItem` prop to render each item based on its `type` property.
- Render different components for each item type (e.g., `TextItem`, `ImageItem`, `VideoItem`).

Example `renderItem` implementation:

javascript
const renderItem = ({ item }) => {
  switch (item.type) {
    case 'text':
      return <TextItem item={item} />;
    case 'image':
      return <ImageItem item={item} />;
    case 'video':
      return <VideoItem item={item} />;
    default:
      return null;
  }
};

3. Handling Section Headers:
- Use the `renderSectionHeader` prop to render the section headers.
- You can use the `section` object to access the `title` of each section.

Example `renderSectionHeader` implementation:

javascript
const renderSectionHeader = ({ section }) => (
  <View style={styles.sectionHeader}>
    <Text style={styles.sectionHeaderText}>{section.title}</Text>
  </View>
);

4. Providing a Unique Key:
- Use the `keyExtractor` prop to provide a unique key for each item.
- This is important for efficient rendering and updating of the list.

Example `keyExtractor` implementation:

javascript
const keyExtractor = (item) => item.id.toString();

Here's a complete example of a `SectionList` with heterogeneous data:

javascript
import React from 'react';
import { SectionList, View, Text, StyleSheet } from 'react-native';
import TextItem from './TextItem';
import ImageItem from './ImageItem';
import VideoItem from './VideoItem';

const sections = [
  {
    title: 'Section 1',
    data: [
      { id: 1, type: 'text', content: 'Item 1' },
      { id: 2, type: 'image', imageUrl: 'https://example.com/image1.jpg' },
      { id: 3, type: 'video', videoUrl: 'https://example.com/video1.mp4' },
    ],
  },
  {
    title: 'Section 2',
    data: [
      { id: 4, type: 'text', content: 'Item 4' },
      { id: 5, type: 'image', imageUrl: 'https://example.com/image2.jpg' },
    ],
  },
];

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#f0f0f0',
  },
  sectionHeader: {
    backgroundColor: '#e0e0e0',
    padding: 10,
  },
  sectionHeaderText: {
    fontSize: 16,
    fontWeight: 'bold',
  },
});

const App = () => {
  const renderItem = ({ item }) => {
    switch (item.type) {
      case 'text':
        return <TextItem item={item} />;
      case 'image':
        return <ImageItem item={item} />;
      case 'video':
        return <VideoItem item={item} />;
      default:
        return null;
    }
  };

  const renderSectionHeader = ({ section }) => (
    <View style={styles.sectionHeader}>
      <Text style={styles.sectionHeaderText}>{section.title}</Text>
    </View>
  );

  const keyExtractor = (item) => item.id.toString();

  return (
    <View style={styles.container}>
      <SectionList
        sections={sections}
        renderItem={renderItem}
        renderSectionHeader={renderSectionHeader}
        keyExtractor={keyExtractor}
      />
    </View>
  );
};

export default App;

In this example, the `sections` data contains a mix of text, image, and video items. The `renderItem` function checks the `type` property of each item and renders the appropriate component (`TextItem`, `ImageItem`, or `VideoItem`). The `renderSectionHeader` function renders the section headers, and the `keyExtractor` function provides a unique key for each item.

By following these steps, you can effectively handle heterogeneous data in a React Native `SectionList`.

Citations:
[1] https://reactnative.dev/docs/sectionlist
[2] https://stackoverflow.com/questions/44782268/react-native-heterogenous-section-list
[3] https://blog.stackademic.com/optimizing-sectionlist-performance-in-react-native-c7989af94461?gi=754d786d6d36
[4] https://stackoverflow.com/questions/73258161/how-to-integrate-api-data-with-section-list-react-native/73259564
[5] https://github.com/Nozbe/WatermelonDB/issues/228