反应本机应用程序
我正在实现拉动以刷新我的 Flatlist 道具“onRefresh”
,称为函数“刷新控制()”,请参阅下面的代码。
在从 API 获取之前,我需要将状态“刷新”更改为 true。但它会引发最大的更新错误。
export default class NotificationScreen extends Component {
constructor(props) {
super(props)
this.state = {
refreshing: false
}
}
.
.
.
.
.
refreshControl() {
const { refreshing } = this.state;
this.setState({ refreshing : true )}. // throws maximum update error
return (
<RefreshControl
refreshing={refreshing}
onRefresh={fetchNotifications.bind(this)} //fetch from API only when refreshing is true
colors={['#2B70AD']}
/>
);
};
}
否则,如何将我的状态设置为“刷新:true”???请帮忙!!!!
这就是它的修复方式。溶液:
refresh = async() => {
this.setState({refreshing : true})
try {
const notifications = await fetchNotifications();
this.setState({
notifications,
error: null,
refreshing: false
});
} catch (error) {
this.setState({
notifications: [],
error,
refreshing: false
});
}
}
refreshControl() {
const { refreshing } = this.state;
return (
<RefreshControl
refreshing={refreshing}
onRefresh={this.refresh}
colors={['#2B70AD']}
/>
);
};
米脂
人到中年有点甜
摇曳的蔷薇
相关分类