gpt4 book ai didi

reactjs - ReactNative Flatlist onEndReached 不起作用

转载 作者:行者123 更新时间:2023-12-03 13:56:01 24 4
gpt4 key购买 nike

我尝试在 FlatList 的 onEndReached 上调用函数,但它不起作用。

我在最后调用了 this.state.pageNo 并且它没有更新。我想稍后在无限滚动中使用这个逻辑,但现在无法让它工作。

 import React, { Component } from "react";
import {
View,
Text,
StyleSheet,
Button,
TouchableOpacity,
FlatList,
Alert
} from "react-native";

class InfiniteScrollRedux extends Component {
constructor(props) {
super(props);
this.state = {
loading: false,
pageNo: 1,
data: [
{ id: 1, name: "Name01" },
{ id: 2, name: "Name02" },
{ id: 3, name: "Name03" },
{ id: 4, name: "Name04" },
{ id: 5, name: "Name05" },
{ id: 6, name: "Name06" }
]
};
}

myFunction = () => {
this.setState({ pageNo: this.state.pageNo++ });
};

render() {
return (
<View>
<FlatList
data={this.state.data}
renderItem={({ item }) => (
<View style={mystyle.mainCard}>
<Text style={mystyle.titleText}> {item.id} </Text>
<View style={{ marginTop: 200 }} />
<Text style={mystyle.nameText}> {item.name} </Text>
</View>
)}
keyExtractor={item => item.id}
onEndReached={this.myFunction}
onEndThreshold={0}
/>
<Text style={{ margin: 20, padding: 20, fontSize: 20 }}>
{this.state.pageNo}
</Text>
</View>
);
}
}

const mystyle = StyleSheet.create({
mainCard: {
backgroundColor: "#2F4F4F",
marginBottom: 1,
padding: 5
},
titleText: {
fontSize: 20,
color: "#F0FFFF"
},
nameText: {
fontSize: 14,
color: "#F0FFFF"
}
});

export default InfiniteScrollRedux;

最佳答案

就我而言,问题出在nativebase <Content> 。当 <FlatList> 时它会产生问题被用在了里面。解决方案:

<Content
style={{flex: 1}}
contentContainerStyle={{flex: 1}} // important!
>

来源:https://github.com/GeekyAnts/NativeBase/issues/1736

关于reactjs - ReactNative Flatlist onEndReached 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47351592/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com