Skip to content

Instantly share code, notes, and snippets.

@EdgardoRodriguezSolano
Created May 26, 2020 01:02
Show Gist options
  • Save EdgardoRodriguezSolano/3d26bdd5ddeb52d751d02a66de533bc6 to your computer and use it in GitHub Desktop.
Save EdgardoRodriguezSolano/3d26bdd5ddeb52d751d02a66de533bc6 to your computer and use it in GitHub Desktop.
React-native flatlist componente implementation (infinte scroll)
function FlatListComponent({ page, updated, fetchedData, updateState, refreshQuery, navigation, categorieID, subcategories = null}) {
const { loading, error, data } = subcategories ? useQuery(articlesBySubcategories(categorieID, ARTICLES_TO_FETCH * (page - 1)))
: useQuery(articlesByCategories(categorieID, ARTICLES_TO_FETCH * (page - 1)));
if (error) return <Text> { `Error! ${error}` } </Text>
if (!updated && !data) return <ActivityIndicatorComponent />
const newData = subcategories ? data['subcategorias'][0].articulo
: data['categorias'][0].articulo;
const hasMore = !(newData.length < ARTICLES_TO_FETCH);
if (!updated && !loading && data) updateState(newData);
return (
<FlatList
style={{ width: '100%' }}
data={fetchedData}
keyExtractor={ ( item, index ) => index }
onEndReachedThreshold={0.5}
onEndReached={() => hasMore && refreshQuery()}
ListHeaderComponent={() =>
<View style={ styles.container }>
<Image size={ relativeWidth( 20 ) } style={ styles.image } source={ navigation.state.params.icon } />
<Text style={ styles.header }>
{ navigation.state.params.title || ''}
</Text>
<Text style={ styles.description }>
{ navigation.state.params.description || '' }
</Text>
</View>
}
renderItem={ ({ item, index }) =>
<CardView
data={item}
index={index}
goToArticles={null}
goToChapters={null}
/>
}
ListFooterComponent={() => {
if (!loading && !hasMore) return null;
return (<ActivityIndicatorComponent/>);
}}
/>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment