【问题标题】:React Navigation: Detect to which screen the app was navigatedReact Navigation:检测应用导航到哪个屏幕
【发布时间】:2019-03-27 19:21:48
【问题描述】:

我有 2 个组件:

  1. 仪表板 - 应用程序的入口点
  2. 帖子

在仪表板中,componentDidMount 中有一个 API 调用。 在帖子组件中,收到帖子后,我导航到仪表板。是否可以检测应用程序是否从 Posts 导航到 Dashboard 并删除 componentDidMount 中的 API 调用。

检查下面的代码:

// Dashboard.js

 componentDidMount() {
   this.handleApiCall(); // default axios get request
// Here I need to detect if the user was navigated to Dashboard from Posts or other component
}


// Posts.js
  handleNavigation = () => {
    this.setState({
      isOpen: false,
    });
      this.props.navigation.navigate('Dashboard');
 };

谢谢!

【问题讨论】:

  • 也许你可以从posts.js传递参数? this.props.navigation.navigate('RouteName', { fromPosts: true })

标签: javascript reactjs react-native react-navigation react-props


【解决方案1】:

尝试从Posts.js传递参数

// Dashboard.js
componentDidMount() {

   const { navigation } = this.props;
   const fromPosts = navigation.getParam('fromPosts', false);
   if(!fromPosts) {
     this.handleApiCall(); // default axios get request
   }

}


// Posts.js
handleNavigation = () => {
    this.setState({
      isOpen: false,
    });

    this.props.navigation.navigate('Dashboard', {fromPosts: true});
 };

【讨论】:

    猜你喜欢
    • 2022-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-29
    • 2019-07-08
    • 2020-08-24
    • 2021-08-19
    相关资源
    最近更新 更多