【问题标题】:react navigation performing function when returning from the screen从屏幕返回时反应导航执行功能
【发布时间】:2018-06-27 14:19:10
【问题描述】:
我正在使用反应导航在我的屏幕之间导航。我有一个主屏幕 (Main.js) 和一个名为 (Favorites.js) 的屏幕。
我使用此功能转到我的收藏夹屏幕。
_btnFavoritos = async () => {
const movies = this.state.myFavorites;
console.log("Abrir Favoritos");
console.log(movies);
this.props.navigation.navigate('screenFavoritos', { movies: movies });
};
如何有一个回调函数,以便当我退出我的收藏夹屏幕并返回我的主屏幕时,我会触发一个能够更新我的主屏幕的函数?
【问题讨论】:
标签:
react-native
react-navigation
【解决方案1】:
超出主题,但我会从 _btnFavoritos 中删除 async 关键字,除非您的函数有更多需要它的代码,而您根本没有在此处添加它
解决办法:
_btnFavoritos = () => {
const movies = this.state.myFavorites;
console.log("Abrir Favoritos");
console.log(movies);
this.props.navigation.navigate('screenFavoritos',
{
movies: movies,
updateMain: () => this.updateMain()
});
};
updateMain = () => {
// Sample update logic
fetch('https://someEndpoint.com')
.then(res => res.json())
.then(json => this.setState({ movies: json })
.catch(err => console.warn(err.message)
}
Favorites.js
componentWillUnmount(){
const { params } = this.props.navigation.state
params && params.updateMain && params.updateMain() // Checking that the function was passed down
// as a navigation prop before executing
}