问题
Perviously when I wanted to make some actions when screen is opened I put them inside componentDidMount. For example I can fetch some data.
like this.
componentDidMount() {
this.updateData();
}
But with react-navigation componentDidMount occurs only one time when user open screen first time, and if later user open this page again it will not trigger componentDidMount.
What is proper way to detect when page(screen) is activated and do actions?
回答1:
With react-navigation
, you can do that.
Sample code - done in 2 steps :
Add listeners in
componentDidMount
orcomponentWillMount
this.subs = [ this.props.navigation.addListener('didFocus', (payload) => this.componentDidFocus(payload)), ];
or
this.subs = [ this.props.navigation.addListener('didFocus', this.componentDidFocus), this.props.navigation.addListener('willBlur', this.componentWillBlur), ];
Then you can do anything in
componentDidFocus
, like fetching data, updating data,...In
componentWillUnmount
, remove listenerscomponentWillUnmount() { this.subs.forEach(sub => sub.remove()); }
API Reference Documents :
React-navigation v3.x, v4.x:
addListener
- Subscribe to updates to navigation lifecycleReact Navigation emits events to screen components that subscribe to them:
willFocus
- the screen will focusdidFocus
- the screen focused (if there was a transition, the transition completed)willBlur
- the screen will be unfocuseddidBlur
- the screen unfocused (if there was a transition, the transition completed)
React-navigation 3.x, 4.x example:
const didBlurSubscription = this.props.navigation.addListener(
'didBlur',
payload => {
console.debug('didBlur', payload);
}
);
// Remove the listener when you are done
didBlurSubscription.remove();
JSON payload:
{
action: { type: 'Navigation/COMPLETE_TRANSITION', key: 'StackRouterRoot' },
context: 'id-1518521010538-2:Navigation/COMPLETE_TRANSITION_Root',
lastState: undefined,
state: undefined,
type: 'didBlur',
};
Ref: https://reactnavigation.org/docs/en/navigation-prop.html#addlistener-subscribe-to-updates-to-navigation-lifecycle
回答2:
componentDidMount
/ componentWillUnmount
does not work in all cases of navigation (like tabs).
You need to use addListener
with events didFocus and didBlur to make such actions. See documentation for details
回答3:
This might be late but this is how I solved it. See the code below. Don't forget to import withNavigation and wrap your export default withNavigation.
import { withNavigation } from "react-navigation";
componentDidMount() {
const { navigation } = this.props;
this.focusListener = navigation.addListener("didFocus", () => {
// The screen is focused
// Call any action
});
}
componentWillUnmount() {
// Remove the event listener
this.focusListener.remove();
}
export default withNavigation(Your Class Name);
回答4:
NavigationEvents
is another way to add event listener just inside JSX. See documentation for details.
It looks something like this:
import { NavigationEvents } from 'react-navigation';
return (
<ScrollView style={styles.container}>
<NavigationEvents
onDidBlur={payload => console.log('did blur', payload)}
/>
{this.props.children}
</ScrollView>
);
来源:https://stackoverflow.com/questions/50290818/react-navigation-detect-when-screen-tabbar-is-activated-appear-focus-blu