Created
September 20, 2019 13:42
-
-
Save AliYmn/ad87c4340770132d54882e6e0239eb49 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { | |
SafeAreaView, | |
TouchableOpacity, | |
FlatList, | |
StyleSheet, | |
Text, | |
} from 'react-native'; | |
import Constants from 'expo-constants'; | |
const DATA = [ | |
{ | |
id: 'bd7acbea-c1b1-46c2-aed5-3ad53abb28ba', | |
title: 'First Item', | |
}, | |
{ | |
id: '3ac68afc-c605-48d3-a4f8-fbd91aa97f63', | |
title: 'Second Item', | |
}, | |
{ | |
id: '58694a0f-3da1-471f-bd96-145571e29d72', | |
title: 'Third Item', | |
}, | |
]; | |
function Item({ id, title, selected, onSelect }) { | |
return ( | |
<TouchableOpacity | |
onPress={() => onSelect(id)} | |
style={[ | |
styles.item, | |
{ backgroundColor: selected ? '#6e3b6e' : '#f9c2ff' }, | |
]} | |
> | |
<Text style={styles.title}>{title}</Text> | |
</TouchableOpacity> | |
); | |
} | |
export default function App() { | |
const [selected, setSelected] = React.useState(new Map()); | |
const onSelect = React.useCallback( | |
id => { | |
const newSelected = new Map(selected); | |
newSelected.set(id, !selected.get(id)); | |
setSelected(newSelected); | |
}, | |
[selected][0], | |
); | |
return ( | |
<SafeAreaView style={styles.container}> | |
<FlatList | |
data={DATA} | |
renderItem={({ item }) => ( | |
<Item | |
id={item.id} | |
title={item.title} | |
selected={!!selected.get(item.id)} | |
onSelect={onSelect} | |
/> | |
)} | |
keyExtractor={item => item.id} | |
extraData={selected} | |
/> | |
</SafeAreaView> | |
); | |
} | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
marginTop: Constants.statusBarHeight, | |
}, | |
item: { | |
backgroundColor: '#f9c2ff', | |
padding: 20, | |
marginVertical: 8, | |
marginHorizontal: 16, | |
}, | |
title: { | |
fontSize: 32, | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment