[英]How to reload a page, when user navigates back to the page from another page in React Native?
I have two pages, home page and search page .我有两个页面,主页和搜索页面。 The scenario is I'm navigating from home page to search page, in the search page I'm getting some search results.
场景是我从主页导航到搜索页面,在搜索页面中我得到了一些搜索结果。 I'm passing the search results back to the home page using navigation like this:
我正在使用如下导航将搜索结果传回主页:
this.props.navigation.navigate('Home', {
searchResponse: data,
});
This takes me to the home page, but it is not getting reloaded.这会将我带到主页,但它没有重新加载。 How do I refresh the home page, so that I can make use of the data received from the search page?
如何刷新主页,以便我可以使用从搜索页面收到的数据?
Try using replace method尝试使用替换方法
this.props.navigation.replace('Home', { searchResponse: data, });
This will replace the entire stack of url's with a new one这将用新的 url 替换整个 url 堆栈
Just try with this once, since navigation.navigate doesnt reload the page and it fetches the page from the stack , so try with .push which explictly reloads the page:只需尝试一次,因为 navigation.navigate 不会重新加载页面,而是从堆栈中获取页面,所以尝试使用 .push 显式重新加载页面:
this.props.navigation.push('Home', {
searchResponse: data,
});
UPDATE:更新:
if you dont want to use push, react navigation has this new feature :如果你不想使用推送,反应导航有这个新功能:
import { StackActions } from '@react-navigation/native';
this.props.navigation.dispatch(
StackActions.replace('Home', {
searchResponse: data,
})
);
For more details check this rn-docs有关更多详细信息,请查看此rn-docs
Hope it help.s希望能帮助到你
you can use add navigation.addListener inside componentDidMount in your screen its make every time you naviage to screen read ComponentDidMount method like您可以在屏幕中的 componentDidMount 内使用 add navigation.addListener 每次导航到屏幕读取 ComponentDidMount 方法时,
{ componentDidMount() { { componentDidMount() {
const { navigation } = this.props;
navigation.addListener('willFocus', () => {
console.log("ComponentDidMount function")
})
}
} in this example every time you navigate to screen it console ComponentDidMount function在这个例子中每次你导航到屏幕它控制台 ComponentDidMount 函数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.