javascript - react 导航 : Detect to which screen the app was navigated

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

我有两个组件:

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

在 Dashboard 中,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 传递参数

// 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});
 };

关于javascript - react 导航 : Detect to which screen the app was navigated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55384994/

相关文章:

javascript - 参数从按钮到模态,然后调用 Spring Controller

javascript - 使用 react-infinite-scroll-component 的 Firestore 限制仅在给定限制下滚动

reactjs - 使用 Formik 在 React Native 中处理 DateTimePicker 的值

javascript - 在react.js中过滤记录

Javascript 将 JSON 字符串化两次

javascript - 更改表行的颜色加载

javascript - 三个js渲染一个静态场景

javascript - 文件上传阻止更大的文件

javascript - setState 从回调内部呈现 React 组件中的相关内容

javascript - 如何补偿圆形 slider 组件中的旋转容器 View ?