Cannot get current scroll height from onScroll within FlatList
The problem
I cannot get current scroll height from FlatList.
How to reproduce
Simplified test case:
Steps to reproduce:
- Create a constant using hooks to maintain the scroll position with initial value zero.
const [scrollPosition,setScrollPosition]=React.useState(0)
- Create a method for updating the scroll postion. When the user scrolled.
const handleScroll=(event)=>{
let yOffset=event.nativeEvent.contentOffset.y / 1;
setScrollPosition(yOffset)
}
- Call the handleScroll() method when the user scrolls the flat list.
return <View style={{ flex: 1, flexGrow: 1 }}>
<FlatList
onScroll={(event)=>handleScroll(event)}
numColumns={2}
keyExtractor={(item) => item.Id}
data={data}
renderItem={(item) => {
return <View>
<Text>name is {" " + item.name}</Text>
<Text>age is {" " + item.age}</Text>
</View>
}}
/>
<Text>{scrollPosition}</Text>
</View>
Expected behavior
Environment (include versions). Did this work in previous versions? "firebase": "8.2.3", "react-is": "^16.8.0", "expo-font": "~9.2.1", "expo-status-bar": "~1.0.4", "styled-components": "^5.3.3", "@expo/vector-icons": "^12.0.0", "react-native-screens": "~3.4.0", "react-native-elements": "^3.4.2", "react-native-tab-view": "^3.1.1", "react-native-pager-view": "5.0.12", "@react-navigation/native": "^6.0.6", "react-native-toast-message": "^1.6.0", "react-native-confirm-dialog": "^0.1.0", "react-native-safe-area-context": "3.2.0", "@react-native-community/checkbox": "^0.5.8", "@react-navigation/material-top-tabs": "^6.0.6"
@isekaimaou1109 this seems to be working for me Try scrolling the list here. Also, sharing a reproducible demo would be helpful to debug it.
Updating component state on every scroll is very inefficient. Don't do that in your actual app.
@isekaimaou1109 How did you make styled-components work on react-native-web?