App.js
import React from 'react';
import {
SafeAreaView,
View,
FlatList,
StyleSheet,
Text,
StatusBar,
} from 'react-native';
const DATA = [
{
id: '1',
title: 'First Item',
},
{
id: '1',
title: 'First Item',
},
{
id: '1',
title: 'First Item',
},
{
id: '1',
title: 'First Item',
},
{
id: '1',
title: 'First Item',
},
{
id: '2',
title: 'Second Item',
},
{
id: '2',
title: 'Second Item',
},
{
id: '2',
title: 'Second Item',
},
{
id: '2',
title: 'Second Item',
},
{
id: '3',
title: 'Third Item',
},
{
id: '3',
title: 'Third Item',
},
{
id: '3',
title: 'Third Item',
},
{
id: '3',
title: 'Third Item',
},
{
id: '3',
title: 'Third Item',
},
{
id: '3',
title: 'Third Item',
},
{
id: '3',
title: 'Third Item',
},
];
const Item = ({ title }) => (
<View style={styles.item}>
<Text style={styles.title}>{title}</Text>
</View>
);
const Render_FlatList_Sticky_header = () => {
return (
<>
<Text>hello</Text>
</>
);
};
const App = () => {
return (
<SafeAreaView style={styles.container}>
<FlatList
data={DATA}
renderItem={({ item }) => <Item title={item.title} />}
keyExtractor={(item) => item.id}
stickyHeaderIndices={[0]}
ListHeaderComponent={Render_FlatList_Sticky_header}
/>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: StatusBar.currentHeight || 0,
},
item: {
backgroundColor: '#f9c2ff',
padding: 20,
marginVertical: 8,
marginHorizontal: 16,
},
title: {
fontSize: 32,
},
});
export default App;
it’s working with a sticky Header but I want different header when item.id is different
like
-
if Item.id=1 then my sticky Header text is "A",
-
if Item.id=2 then my sticky Header text is "B",
-
if Item.id=3 then my sticky Header text is "B"
I try to do that but it gives me an error
anyone can help me??🐼
3
Answers
You can do something like this:
You can play with the visible item and get desired output you want.
You want this
if Item.id=1 then my sticky Header text is "A",
Then now you have the top item so add the switch case in
Render_FlatList_Sticky_header
based on theViewable
variable.as this:
you can use the state to set the header dynamically.
I guess this is what you want to achieve