Reputation: 2464
I have 2 components:
In Dashboard there's an API call in componentDidMount. In Posts component, after receiving posts I navigate to Dashboard. Is it possible to detect if the app was navigated from Posts to Dashboard and remove API call in componentDidMount.
Check the code below:
// 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');
};
Thanks!
Upvotes: 3
Views: 514
Reputation: 2302
Try passing the parameters from 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});
};
Upvotes: 2