【问题标题】:ReferenceError: Can't find variable: navigation in react nativeReferenceError:找不到变量:在本机反应中导航
【发布时间】:2020-07-04 05:15:58
【问题描述】:

我已经创建了 rncamera 来捕获图片并保存它们,我在相机屏幕上有一个图标应该带我到画廊页面,但是在安装反应导航依赖项后我尝试导航但它一直给我错误“ReferenceError:Can' t 查找变量:在本机反应中导航”。我错过了什么......

 navigatePhotoGallery = () => {
      navigation = useNavigation();
    }

  render() {
    return (
      <View style={styles.container}>
          <RNCamera
            ref = {ref=>{
                this.camera=ref;
            }}
            style={styles.preview}
            flashMode={this.state.flashon}
            type={this.state.backCamera ? RNCamera.Constants.Type.back : RNCamera.Constants.Type.front} 
            captureAudio={this.state.captureAudio}
            androidCameraPermissionOptions={{
              title: 'Vocajam needs ermission to use camera',
              message: 'We need your permission to use your camera',
              buttonPositive: 'Ok',
              buttonNegative: 'Cancel',
            }}
          >
              {
                ({ camera, status, androidRecordAudioPermissionOptions }) => {
                  if (status !== 'READY') return <PendingView />

                  return (
                    <View style={styles.action}>
                         <View style={{ flexDirection: 'row', justifyContent: 'space-between', paddingHorizontal: 20, marginBottom: 15, alignItems: 'flex-end' }}>
                            <TouchableOpacity  onPress={this.toggleTorch.bind(this)}>
                                    { this.state.flashon == RNCamera.Constants.FlashMode.off? (
                                            <Icon
                                                name="md-flash-off"
                                                color="black"
                                                size={30} 
                                            />
                                        ) : (
                                            <Icon
                                                name="md-flash"
                                                color="black"
                                                size={30} 
                                            />
                                        )
                                    }
                                </TouchableOpacity>
                                <View style={{ alignItems: 'center' }}>
                                    <TouchableOpacity onPress={this.takePicture} style={styles.captureBtn} />
                                </View>
                                <View style={{ alignItems: 'center' }}>
                                    <TouchableOpacity onPress={this.recordVideo} style={styles.captureVideoBtn}>
                                      {
                                        this.state.recording ?
                                      (<Text>{this.secondsToMMSS(this.state.seconds)}</Text>) :
                                      (null)
                                      }
                                    </TouchableOpacity>
                                </View>

                                <TouchableOpacity
                                  onPress={this.reverseCamera}
                                  >
                                      <Icon
                                        name="md-reverse-camera"
                                        color="black"
                                        size={30} 
                                      />

                                  </TouchableOpacity>

                          </View>
                    </View>
                  )
                }
              }
              
          </RNCamera>

          <TouchableOpacity 
            style={styles.photoGalleryIcon}
            onPress={() => this.navigatePhotoGallery(navigation.navigate('PhotoGallery'))}
          >
                <Image 
                  source={require('../images/photoGalleryIcon.png')}
                />
            </TouchableOpacity>
         


          
      </View>
    );
  };
}
export default cameraComponent;

【问题讨论】:

  • 引用错误是指在范围内的任何地方都找不到变量。 navigation 可作为 react-navigation 的道具使用,如果 react-navigation 设置正确,则它必须始终可用。我在这里看到的问题是在线的,您使用了 Touchable opacity 您正在访问navigation,但由于这是一个反应道具并且您使用的是基于类的组件,请使用this.props.navigation.navigate('whereToNavigate')

标签: javascript react-native react-native-android react-native-navigation


【解决方案1】:

当你有一个渲染函数时,你正在使用一个基于类的组件 使用

this.props.navigation.navigate('PhotoGallery')

【讨论】:

    猜你喜欢
    • 2020-06-26
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-03
    • 2021-02-14
    相关资源
    最近更新 更多