在react native中从后台模式返回时更新数据



嗨,我在react native中使用地理位置坐标来显示我周围的用户。我实现了一个下拉刷新功能,可以在更改位置后刷新用户列表。从后台模式恢复时,如何刷新数据?

displayPosition = () => {
this.watchId = Geolocation.getCurrentPosition(
(position) => {
let obj = {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
error: null
}
this.setState(obj);
},
(error) => this.setState({ error: error.message }),
{ enableHighAccuracy: true, timeout: 200000, maximumAge: 1000},
);
if(this.state.latitude!=null){
this.getData();
}else{
setTimeout(this.displayPosition.bind(this),200)
}
//console.log(this.state.latitude);
//console.log(this.state.longitude);
}
handleRefresh = () => {
this.setState (
{
refreshing: true,
},
() => {
this.setState({refreshing: false});
this.displayPosition();
//this.getData();
}
);
};

componentDidMount() {
this.displayPosition();
//this.getData();
this.props.navigation.setParams({ refresh: this._handleRefresh });
}
componentWillUnmount() {
Geolocation.clearWatch(this.watchId);
}

谢谢!

您可以在react native中为其添加侦听器

共有4名听众

  1. 将聚焦-屏幕将聚焦
  2. didFocus-屏幕聚焦(如果有转换,则转换完成(
  3. 将模糊-屏幕将不聚焦
  4. didBlur-屏幕未聚焦(如果有转换,则转换完成(


尝试以下代码

componentWillMount(){
const didFocusSubscription = this.props.navigation.addListener(
'didFocus',
payload => {
console.warn('didFocus ', payload);
# just write your code/call a method here which you want to execute when the app comes from background
this.handleRefresh()
}
);
}

做完后别忘了把它取下来。

componentWillUnmount(){
didFocusSubscription.remove();
}

你可以在这里阅读更多内容。谢谢:(

最新更新