Created
May 24, 2019 10:12
-
-
Save ngoryachev/9144c02e532cc859875998e5150c920a to your computer and use it in GitHub Desktop.
This file contains 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
const Tile = ({ id, url, onPress, isSelected }) => { | |
<View | |
source={url} | |
onPress={onPress} | |
/> | |
} | |
const staticData = [ | |
{ id: 1, url: 'someurl1' }, | |
{ id: 2, url: 'someurl2' } | |
] | |
class List extends React.Compnent { | |
state = { selectedIds: [] } | |
handlePress = (id) => { | |
const selectedIds = [...this.state.selectedIds, id] | |
this.setState({ selectedIds }) | |
} | |
isSelected = (id) => selectedIds.includes(id) | |
getData = () => { | |
return staticData.map(({ id, url }) => ({ | |
id, | |
url, | |
onPress: () => this.handlePress(id), | |
isSelected: this.isSelected(id), | |
}) | |
} | |
renderItem = ({ item }) => ( | |
<Tile {...item}> | |
) | |
render () => { | |
return ( | |
<FlatList | |
data={this.getData()} | |
renderItem={this.renderItem} | |
/> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment