Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 131 additions & 39 deletions docs/sectionlist.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,80 +20,172 @@ If you don't need section support and want a simpler interface, use [`<FlatList>

## Example

```SnackPlayer name=SectionList
import React from 'react';
<div class="toggler">
<ul role="tablist" class="toggle-syntax">
<li id="functional" class="button-functional" aria-selected="false" role="tab" tabindex="0" aria-controls="functionaltab" onclick="displayTabs('syntax', 'functional')">
Function Component Example
</li>
<li id="classical" class="button-classical" aria-selected="false" role="tab" tabindex="0" aria-controls="classicaltab" onclick="displayTabs('syntax', 'classical')">
Class Component Example
</li>
</ul>
</div>

<block class="functional syntax" />

```SnackPlayer name=SectionList%20Example
import React from "react";
import {
StyleSheet,
Text,
View,
SafeAreaView,
SectionList,
} from 'react-native';
import Constants from 'expo-constants';
SectionList
} from "react-native";
import Constants from "expo-constants";

const DATA = [
{
title: 'Main dishes',
data: ['Pizza', 'Burger', 'Risotto'],
title: "Main dishes",
data: ["Pizza", "Burger", "Risotto"]
},
{
title: 'Sides',
data: ['French Fries', 'Onion Rings', 'Fried Shrimps'],
title: "Sides",
data: ["French Fries", "Onion Rings", "Fried Shrimps"]
},
{
title: 'Drinks',
data: ['Water', 'Coke', 'Beer'],
title: "Drinks",
data: ["Water", "Coke", "Beer"]
},
{
title: 'Desserts',
data: ['Cheese Cake', 'Ice Cream'],
title: "Desserts",
data: ["Cheese Cake", "Ice Cream"]
}
];

const Item = ({ title }) => (
<View style={styles.item}>
<Text style={styles.title}>{title}</Text>
</View>
);

const App = () => (
<SafeAreaView style={styles.container}>
<SectionList
sections={DATA}
keyExtractor={(item, index) => item + index}
renderItem={({ item }) => <Item title={item} />}
renderSectionHeader={({ section: { title } }) => (
<Text style={styles.header}>{title}</Text>
)}
/>
</SafeAreaView>
);

export default App;

const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: Constants.statusBarHeight,
marginHorizontal: 16
},
item: {
backgroundColor: "#f9c2ff",
padding: 20,
marginVertical: 8
},
header: {
fontSize: 32,
backgroundColor: "#fff"
},
title: {
fontSize: 24
}
});
```

<block class="classical syntax" />

```SnackPlayer name=SectionList%20Example
import React, { Component } from "react";
import {
StyleSheet,
Text,
View,
SafeAreaView,
SectionList
} from "react-native";
import Constants from "expo-constants";

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"]
}
];

function Item({ title }) {
return (
<View style={styles.item}>
<Text style={styles.title}>{title}</Text>
</View>
);
Item = ({ title }) => (
<View style={styles.item}>
<Text style={styles.title}>{title}</Text>
</View>
);

class App extends Component {
render() {
return (
<SafeAreaView style={styles.container}>
<SectionList
sections={DATA}
keyExtractor={(item, index) => item + index}
renderItem={({ item }) => <Item title={item} />}
renderSectionHeader={({ section: { title } }) => (
<Text style={styles.header}>{title}</Text>
)}
/>
</SafeAreaView>
);
}
}

export default function App() {
return (
<SafeAreaView style={styles.container}>
<SectionList
sections={DATA}
keyExtractor={(item, index) => item + index}
renderItem={({ item }) => <Item title={item} />}
renderSectionHeader={({ section: { title } }) => (
<Text style={styles.header}>{title}</Text>
)}
/>
</SafeAreaView>
);
}
export default App;

const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: Constants.statusBarHeight,
marginHorizontal: 16,
marginHorizontal: 16
},
item: {
backgroundColor: '#f9c2ff',
backgroundColor: "#f9c2ff",
padding: 20,
marginVertical: 8,
marginVertical: 8
},
header: {
fontSize: 32,
backgroundColor: '#fff'
backgroundColor: "#fff"
},
title: {
fontSize: 24,
},
fontSize: 24
}
});
]
```

<block class="endBlock syntax" />

This is a convenience wrapper around [`<VirtualizedList>`](virtualizedlist.md), and thus inherits its props (as well as those of [`<ScrollView>`](scrollview.md) that aren't explicitly listed here, along with the following caveats:

- Internal state is not preserved when content scrolls out of the render window. Make sure all your data is captured in the item data or external stores like Flux, Redux, or Relay.
Expand Down