Skip to content

Commit

Permalink
imp: Snack example for SectionList (facebook#1247)
Browse files Browse the repository at this point in the history
* imp: Snack example for SectionList

* Revert prettier mess
  • Loading branch information
Esemesek authored and charpeni committed Sep 13, 2019
1 parent 703cc58 commit 5edea43
Showing 1 changed file with 73 additions and 28 deletions.
101 changes: 73 additions & 28 deletions docs/sectionlist.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,79 @@ If you don't need section support and want a simpler interface, use [`<FlatList>

Simple Examples:

```jsx
// Example 1 (Homogeneous Rendering)
<SectionList
renderItem={({item, index, section}) => <Text key={index}>{item}</Text>}
renderSectionHeader={({section: {title}}) => (
<Text style={{fontWeight: 'bold'}}>{title}</Text>
)}
sections={[
{title: 'Title1', data: ['item1', 'item2']},
{title: 'Title2', data: ['item3', 'item4']},
{title: 'Title3', data: ['item5', 'item6']},
]}
keyExtractor={(item, index) => item + index}
/>
```

```jsx
// Example 2 (Heterogeneous Rendering / No Section Headers)
const overrideRenderItem = ({ item, index, section: { title, data } }) => <Text key={index}>Override{item}</Text>

<SectionList
renderItem={({ item, index, section }) => <Text key={index}>{item}</Text>}
sections={[
{ title: 'Title1', data: ['item1', 'item2'], renderItem: overrideRenderItem },
{ title: 'Title2', data: ['item3', 'item4'] },
{ title: 'Title3', data: ['item5', 'item6'] },
]}
/>
### Example

```SnackPlayer name=SectionList
import React 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>
);
}
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>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: Constants.statusBarHeight,
marginHorizontal: 16,
},
item: {
backgroundColor: '#f9c2ff',
padding: 20,
marginVertical: 8,
},
header: {
fontSize: 32,
},
title: {
fontSize: 24,
},
});
```

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:
Expand Down

0 comments on commit 5edea43

Please sign in to comment.