Last active
May 16, 2020 21:57
-
-
Save topherPedersen/258f033cbd48feacfcccdf1a4ba8ca91 to your computer and use it in GitHub Desktop.
Skip over item or row in React Native FlatList
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
import React from 'react'; | |
import { | |
SafeAreaView, | |
StyleSheet, | |
ScrollView, | |
View, | |
Text, | |
StatusBar, | |
FlatList, | |
} from 'react-native'; | |
// SEE BLOG POST FOR EXPLANATION REGARDING HOW TO SKIP | |
// OVER CERTAIN ITEMS AND ROWS IN A REACT NATIVE FLAT LIST | |
// https://topherpedersen.blog/2020/05/16/how-to-skip-filter-and-remove-items-and-rows-when-rendering-a-flatlist-in-react-native/ | |
class FlatListItem extends React.Component { | |
constructor(props) { | |
super(props); | |
} | |
render() { | |
if (this.props.text !== "bar") { | |
return( | |
<View> | |
<Text>{this.props.text}</Text> | |
</View> | |
); | |
} else { | |
return( | |
<></> | |
); | |
} | |
} | |
} | |
const Empty = <></>; | |
class App extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
items: [ | |
{text: "foo"}, | |
{text: "bar"}, | |
{text: "baz"}, | |
], | |
} | |
} | |
render() { | |
return( | |
<SafeAreaView style={{flex: 1}}> | |
<View style={{flex: 15}}> | |
<Text style={{textAlign: 'center'}}>FlatListApp</Text> | |
</View> | |
<View style={{flex: 85, justifyContent: 'center', backgroundColor: 'white'}}> | |
<FlatList | |
key="flat-list-key" | |
data={this.state.items} | |
renderItem={({ item }) => ( | |
<FlatListItem text={item.text} /> | |
)} | |
keyExtractor={ (item, index) => index.toString() }/> | |
</View> | |
</SafeAreaView> | |
); | |
} | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment