简体   繁体   English

当用户从 React Native 中的另一个页面导航回页面时,如何重新加载页面?

[英]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.

相关问题 如何在重新加载页面时清除HTML表单,但如何在用户导航回该页面时清除? - How do you clear an HTML form on page reload but not when the user navigates BACK to the page? 检测用户何时离开页面 - Detect when user navigates away from page 用户导航到其他页面并返回时不清除表单数据 - Not clearing form data when user navigates to other page and comes back 如果用户离开页面,如何维护页面状态? - How to maintain page state if user navigates away from page? React Native/Firebase/Expo Audio - 在下载开始并且用户离开页面后取消 loadAsync - React Native/Firebase/Expo Audio - Cancel loadAsync after download starts and user navigates away from page 当用户导航到另一个页面时自动开始下载 - Start download automatically when a user navigates to another page 为什么当用户离开页面导航时不触发回发,而是在关闭时触发回发? - Why is Post back not triggered when user navigates away from page, but triggered on close? React&MobX-用户离开现有页面时的“确认”对话框 - React & MobX - Confirmation dialog when a user navigates away from the existing page 用户单击浏览器的后退按钮时如何重新加载当前页面 - How to reload current page when user click back button of browser 用户提交后退按钮时如何重新加载同一页面 - How to reload the same page when user submits the back button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM