React Navigation:检测应用程序导航到哪个屏幕
React Navigation: Detect to which screen the app was navigated
我有 2 个组成部分:
- 仪表板 - 应用的入口点
- 帖子
在 Dashboard 中有一个 API 调用 componentDidMount。
在帖子组件中,收到帖子后,我导航到仪表板。是否可以检测应用程序是否从帖子导航到仪表板并删除 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});
};
我有 2 个组成部分:
- 仪表板 - 应用的入口点
- 帖子
在 Dashboard 中有一个 API 调用 componentDidMount。 在帖子组件中,收到帖子后,我导航到仪表板。是否可以检测应用程序是否从帖子导航到仪表板并删除 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});
};