【发布时间】: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