在React native中基于特定的滚动位置执行一个函数



我正在尝试调用基于滚动视图当前位置的api,但不确定如何实现这一目标。

这是我的代码

<ScrollView
ref={scrollViewRef}
scrollEventThrottle={0}
onScroll={({nativeEvent}) => {
console.log(
nativeEvent.contentSize.height -
nativeEvent.layoutMeasurement.height,
);
console.log(nativeEvent.contentOffset);
}}>

我试着在onScroll里面调用api,但是没有很好地工作。

尝试在您希望函数执行的特定滚动位置添加事件侦听器。

useEffect(() => {
Window.addEventListener(‘scroll’_.debounce(setScroll,1000));},[]);

我通过添加if检查解决了这个问题。如果api数据存在,那么函数将不再执行。下面是代码

const [apiData, setApiData] = useState();
onScroll={({nativeEvent}) => {
if (!apiData) {
if (
nativeEvent.contentSize.height -
nativeEvent.layoutMeasurement.height -
nativeEvent.contentOffset.y <
250
) {
getApiDataHandler();
}
}
}}

最新更新