我們假設(shè)B頁(yè)面返回到A頁(yè)面。
方法一
在A頁(yè)面中設(shè)置監(jiān)聽(tīng)器 :
componentDidMount(){
this.backFromShopListener = DeviceEventEmitter.addListener(
'BackRefresh', //監(jiān)聽(tīng)器名
() => {
this.getDeviceList(); //此處寫(xiě)你的得到列表數(shù)據(jù)的函數(shù)
},
);
}
componentWillUnmount() {
this.backFromShopListener && this.backFromShopListener.remove();
}
在B頁(yè)面中發(fā)送通知:
import { DeviceEventEmitter} from 'react-native';
// 頁(yè)面銷(xiāo)毀時(shí)發(fā)送通知
componentWillUnmount() {
DeviceEventEmitter.emit('BackRefresh', {});
}
方法二
封裝監(jiān)聽(tīng)組件PageListen
import React, {Component} from 'react';
import {useFocusEffect} from '@react-navigation/native';
//監(jiān)聽(tīng)頁(yè)面返回 刷新數(shù)據(jù)
const PageListen = ({onUpdate}) => {
useFocusEffect(
React.useCallback(() => {
onUpdate();
return () => {
// Do something when the screen is unfocused
};
}, [onUpdate]),
);
return null;
};
export default PageListen;
渲染出來(lái)
<PageListen onUpdate={this._onUpdate} />
_onUpdate() {
console.log('返回時(shí)刷新頁(yè)面');
}