请帮我实现拉动刷新我的应用程序,我有点新的反应原生,谢谢。我不知道如何处理onRefresh和刷新。
class HomeScreen extends Component {
state = { refreshing: false }
_renderItem = ({ item }) => <ImageGrid item={item} />
_handleRefresh = () => {
};
render() {
const { data } = this.props;
if (data.loading) {
return (
<Root>
<Loading size="large" />
</Root>
)
}
return (
<Root>
<HomeHeader />
<FlatList
contentContainerStyle={{ alignSelf: 'stretch' }}
data={data.getPosts}
keyExtractor={item => item._id}
renderItem={this._renderItem}
numColumns={3}
refreshing={this.state.refreshing}
onRefresh={this._handleRefresh}
/>
</Root>
);
}
}
export default graphql(GET_POSTS_QUERY)(HomeScreen);
答案 0 :(得分:28)
设置变量
this.state = {
isFetching: false,
}
创建刷新功能
onRefresh() {
this.setState({ isFetching: true }, function() { this.getApiData() });
}
最后设置onRefresh并在FlatList中刷新。
<FlatList
data={ this.state.FlatListItems }
onRefresh={() => this.onRefresh()}
refreshing={this.state.isFetching}
在函数getApiData中获取数据后,请确保设置为false isFetching。
this.setState({ isFetching: false })
答案 1 :(得分:2)
您还可以在Flatlist ScrollView和任何其他列表组件中使用refreshControl
<FlatList
contentContainerStyle={{ alignSelf: 'stretch' }}
data={data.getPosts}
keyExtractor={item => item._id}
renderItem={this._renderItem}
numColumns={3}
refreshControl={
<RefreshControl
refreshing={this.state.refreshing}
onRefresh={this._handleRefresh}
/>
}
/>
答案 2 :(得分:1)
不用在下拉列表上刷新即可,只需在滚动视图上使用即可。
设置刷新状态
this.state = {
isFetching: false,
}
在ScrollView中设置onFresh
<ScrollView
refreshControl={
<RefreshControl
refreshing={this.state.refresh}
onRefresh={() => this.onRefresh()}
tintColor="red"
/>
}>
<FlatList data={data} renderItem={this._renderItem} numColumns={3} />
</ScrollView>;
在OnRefreshFunction中
onRefresh() {
this.setState({refresh: true});
setTimeout(() => {
this.setState({refresh: false});
}, 2000);
}
答案 3 :(得分:0)
“拉到刷新”概念意味着可以手动刷新列表,因此可以在当前视图之外进行更改(例如,从服务器获取)。因此回调onRefresh
必须触发数据重新加载过程(例如,向服务器发送请求)并将refreshing
变量设置为truthy值。这将通过显示加载指示器通知用户该过程已启动。准备好数据后,您需要将refreshing
设置为falsy,以便视图隐藏加载指示符。
答案 4 :(得分:0)
//样本代码表示在列表中刷新的拉动。相应地修改。
import React, { Component } from 'react'
import { Text, View,StyleSheet,FlatList,Dimensions,Image,TouchableHighlight } from 'react-native'
export default class Home extends Component {
constructor(props)
{
super(props);
this.state = {
data : [],
gender : "",
isFetching: false,
}
}
componentWillMount()
{
this.searchRandomUser()
}
onRefresh() {
this.setState({ isFetching: true }, function() { this.searchRandomUser() });
}
searchRandomUser = async () =>
{
const RandomAPI = await fetch('https://randomuser.me/api/?results=20')
const APIValue = await RandomAPI.json();
const APIResults = APIValue.results
console.log(APIResults[0].email);
this.setState({
data:APIResults,
isFetching: false
})
}
render() {
return (
<View style = {styles.container}>
<TouchableHighlight
onPressOut = {this.searchRandomUser}
style = {{width:deviceWidth - 32, height:45,backgroundColor: 'green',justifyContent:"center",alignContent:"center"}}>
<Text style = {{fontSize:22,color: 'white',fontWeight: 'bold',textAlign:"center"}}>Reload Data</Text>
</TouchableHighlight>
<FlatList
data={this.state.data}
onRefresh={() => this.onRefresh()}
refreshing={this.state.isFetching}
keyExtractor = { (item, index) => index.toString() }
renderItem={({item}) =>
<View style = {styles.ContainerView}>
<View>
<Image
source={{uri : item.picture.large}}
style = {{height:100,width:100,borderRadius: 50,marginLeft: 4}}
resizeMode='contain'
/>
</View>
<View style = {{flexDirection: 'column',marginLeft:16,marginRight: 16,flexWrap:'wrap',alignSelf:"center",width: deviceWidth-160}}>
<Text>Email Id : {item.email}</Text>
<Text>Full Name : {this.state.gender} {item.name.first} {item.name.last}</Text>
<Text>Date of birth : {item.dob.age}</Text>
<Text>Phone number : {item.phone}</Text>
</View>
</View>
}
/>
</View>
)
}
}
const deviceWidth = Dimensions.get('window').width
const deviceHeight = Dimensions.get('window').height
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
marginTop:22
},
ContainerView:
{
// backgroundColor:'grey',
marginBottom:20,
paddingVertical:10,
backgroundColor: '#F5F5F5',
borderBottomWidth:0.5,
borderBottomColor:'grey',
width: deviceWidth-40,
marginLeft:20,
marginRight:20,
marginTop:20,
flexDirection:'row'
}
});
答案 5 :(得分:0)
const [refreshing, setRefreshing] = useState(false)
<FlatList
style={{ padding: 15 }}
refreshControl={
<RefreshControl refreshing={refreshing}
onRefresh={_onRefresh}tintColor="#F8852D"/>
}
showsVerticalScrollIndicator={false}
data={listData}
renderItem={renderItem}
item={(item, index) => index.toStirng()}
onEndReachedThreshold={0.8}
onEndReached={() => onEndReached()}
ListFooterComponent={() => renderFooter()}
/>
<块引用>
_onRefresh 函数
const _onRefresh = () => {
console.log('_onRefresh')
setRefreshing(true);
getPosts();
};
答案 6 :(得分:0)
如果有人对使用 React Hooks 感兴趣,这里是:
首先创建 isRefreshing
状态:const [isRefreshing, setIsRefreshing] = useState(false)
然后创建调用 API 以刷新数据的 onRefresh
函数:
const onRefresh = () => {
//set isRefreshing to true
setIsRefreshing(true)
callApiMethod()
// and set isRefreshing to false at the end of your callApiMethod()
}
之后,您的 FlatList
组件如下所示:
return (
<FlatList
style={styles.flatList}
onRefresh={onRefresh}
refreshing={isRefreshing}
data={data}
renderItem={renderItem}
keyExtractor={item => item.id.toString()}
/>
)
答案 7 :(得分:-1)
这是我能做的最好的事情。 my Code Image
当我把它拉下来时它不会从服务器重新获取数据我正在运行由Apollo连接到应用程序的graphql服务器,我不知道如何从_getData()函数中的服务器获取数据。