我对React Native还是比较陌生的。我正在尝试添加在页面上添加图像的功能,并收到以下错误。
“ TypeError:无法读取未定义的属性'showImagePicker'
这是我的代码:
import React,{Component} from 'react';
import { StyleSheet, Text, View,Image,TouchableOpacity,Alert } from 'react-native';
import * as ImagePicker from 'react-native-image-picker'
const options={
title: 'Chose an Option',
takePhotoButtonTitle:'Use Camera',
chooseFromLibraryButtonTitle:'Select From Gallery',
}
export default class Upload extends Component {
constructor(props){
super(props);
this.state={
avatarSource: null
}
}
uploadImage=()=>{
//alert('Do not toch me ')
ImagePicker.showImagePicker(options, response => {
console.log('Response = ', response);
if (response.didCancel) {
console.log('User cancelled image picker');
} else if (response.error) {
console.log('ImagePicker Error: ', response.error);
} else {
const source = { uri: response.uri };
// You can also display the image using data:
// const source = { uri: 'data:image/jpeg;base64,' + response.data };
this.setState({
avatarSource: source,
});
}
});
}
render(){
return (
<View style={styles.container}>
<TouchableOpacity style={{backgroundColor:'green',margin:10,padding:30}} onPress={this.uploadImage}>
<Text style={{color:'#fff'}}>
Select Image
</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
我还附上了我当前依赖项的列表。
"dependencies": {
"@react-native-community/masked-view": "^0.1.6",
"events": "^3.1.0",
"expo": "~37.0.3",
"oracledb": "^4.2.0",
"path": "^0.12.7",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-native": "https://github.com/expo/react-native/archive/sdk-37.0.1.tar.gz",
"react-native-gesture-handler": "^1.6.1",
"react-native-image-picker": "^0.28.0",
"react-native-reanimated": "^1.7.0",
"react-native-safe-area-context": "^0.7.3",
"react-native-screens": "^2.2.0",
"react-native-web": "^0.12.2",
"react-navigation": "^4.3.7",
"react-navigation-drawer": "^2.4.11",
"react-navigation-stack": "^2.3.11",
"react-navigation-tabs": "^2.8.11",
"stream": "0.0.2",
"util": "^0.12.2"
}
请让我知道如何解决此问题。