Expo Camera only opening once with React Navigation Expo Camera only opening once with React Navigation reactjs reactjs

Expo Camera only opening once with React Navigation


With react navigation 5.x

import { useIsFocused } from '@react-navigation/native';export const CameraView = (props) => {    const isFocused = useIsFocused();    return (       <View>         { isFocused && <RNCamera  />  }       </View>     )}

useIsFocused Documentation


I had some issue.

This code solved it for me:

import { withNavigationFocus } from 'react-navigation'render() {    const { isFocused } = this.props    return (       <View>         { isFocused && <RNCamera  ... />  }       </View     )}export default withNavigationFocus(Camera)


To make this work you need to:

1.

import { NavigationEvents } from 'react-navigation';
state = { loaded: true }
render() {    const { loaded } = this.state;        return (            <View style={styles.container}>            <NavigationEvents             onWillFocus={payload => this.setState({loaded: true})}             onDidBlur={payload => this.setState({loaded: false})}/>            <View style={styles.cameraArea}>            {loaded && (                <Camera                 type={Camera.Constants.Type.back}                 ref={ref => {                     this.camera = ref;                }}              />              )}                      </View>

The idea is to hide this camera view (onDidBlur-> loaded: false), then when you come back (onWillFocus is triggered and change loaded to true). When render() function is called it will show the <Camera /> again.If you have any questions, feel free to ask.