1

I'm developing an application for a school project, and in this application I have a camera component in each view. When I go back on a view the camera freeze.

There is my code of one view:

class LoginView extends React.Component {
  constructor(props) {
    super(props);
  }

  togglePseudo = pseudo => {
    const action = {type: 'USER_PSEUDO', value: pseudo};
    this.props.dispatch(action);
  };

  render() {
    const {navigate} = this.props.navigation;
    const userPseudo = this.props.userPseudo;

    return (
      <View style={style.mainContainer}>
        <RNCamera
          style={style.backgroundCamera}
          type={RNCamera.Constants.Type.back}
          flashMode={RNCamera.Constants.FlashMode.on}
          androidCameraPermissionOptions={{
            title: "Permission d'utiliser la camera",
            message: "L'application necessite l'autorisation de la camera",
            buttonPositive: 'Autoriser',
            buttonNegative: 'Refuser',
          }}
        />
        <View style={style.goContainer}>
          <TouchableOpacity
            style={style.backButton}
            onPress={() => navigate('HomeView')}>
            <Image source={require('assets/images/back-button.png')} />
          </TouchableOpacity>
          <View style={style.centeredElements}>
            <Text style={style.titleMission}>Mission "Pc"</Text>
            <View style={style.modalChoosePseudo}>
              <Text style={style.modaltitle}>Choisir un pseudo</Text>
              <TextInput
                style={style.pseudoInput}
                onChangeText={pseudo => this.togglePseudo(pseudo)}
                value={userPseudo}
              />
              {userPseudo !== '' ? (
                <TouchableOpacity
                  style={style.validateButton}
                  onPress={() => navigate('InGame')}>
                  <Text style={style.buttonText}>Valider</Text>
                </TouchableOpacity>
              ) : (
                <TouchableOpacity
                  style={[style.validateButton, style.validateButtonDisabled]}
                  onPress={() =>
                    Alert.alert('Alerte', 'Veuillez entrer un pseudo')
                  }>
                  <Text
                    style={[style.buttonText, style.validateButtonDisabled]}>
                    Valider
                  </Text>
                </TouchableOpacity>
              )}
            </View>
            <Image
              style={style.logoDimagine}
              source={require('assets/images/logo_title_vertical.png')}
            />
          </View>
        </View>
      </View>
    );
  }
}

I have already looked for solutions, so I tried what I found. I've try to use componentDidMount with willFocus and willBlur, but it never detect them :

componentDidMount() {
    const {navigation} = this.props;
    navigation.addListener('willFocus', () =>
      this.setState({focusedScreen: true}),
    );
    navigation.addListener('willBlur', () =>
      this.setState({focusedScreen: false}),
    );
  }
Anis D
  • 761
  • 11
  • 25

0 Answers0