反应导航:检测应用程序导航到的屏幕

问题描述 投票:3回答:1

我有2个组件:

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

在Dashboard中,componentDidMount中有一个API调用。在帖子组件中,收到帖子后,我导航到仪表板。是否可以检测应用程序是否从帖子导航到仪表板并删除了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');
 };

谢谢!

javascript reactjs react-native react-navigation react-props
1个回答
2
投票

尝试从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});
 };
© www.soinside.com 2019 - 2024. All rights reserved.