我有2个组成部分:仪表板-应用程序的入口点帖子在仪表板中,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'); };
1 回答

慕森卡
TA贡献1806条经验 获得超8个赞
尝试传递来自的参数 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});
};
添加回答
举报
0/150
提交
取消