feat(docs): update react-navigation doc (#2574)

This commit is contained in:
Theodore Sevvos 2019-11-11 16:28:41 +02:00 committed by Sibelius Seraphini
parent cc3016fe43
commit 81563d7a0b

View File

@ -4,29 +4,25 @@ title: React Navigation
sidebar_label: React Navigation
---
React-navigation does not unmount components when switching between tabs. So when you leave and return back to the screen with the camera component it will just be black view. So a good solution is to use `componentDidMount` and added two listeners `willFocus` and `willBlur` to help you mount and unmount the views.
React-navigation does not unmount components when switching between tabs. So when you leave and return back to the screen with the camera component it will just be black view. So a good solution is to use `withNavigationFocus`, which is a higher order component, and wrap it around your component. Then, you can have access to `isFocused` from `props`.
```jsx
componentDidMount() {
const { navigation } = this.props;
navigation.addListener('willFocus', () =>
this.setState({ focusedScreen: true })
);
navigation.addListener('willBlur', () =>
this.setState({ focusedScreen: false })
);
}
import { withNavigationFocus } from 'react-navigation'
render() {
const { hasCameraPermission, focusedScreen } = this.state;
if (hasCameraPermission === null) {
return <View />;
} else if (hasCameraPermission === false) {
return <Text>No access to camera</Text>;
} else if (focusedScreen){
return (this.cameraView());
} else {
return <View />;
}
const { isFocused } = this.props
const { hasCameraPermission } = this.state;
if (hasCameraPermission === null) {
return <View />;
} else if (hasCameraPermission === false) {
return <Text>No access to camera</Text>;
} else if (isFocused){
return (this.cameraView());
} else {
return <View />;
}
}
export default withNavigationFocus(YourComponent)
```