【发布时间】:2019-07-30 07:17:55
【问题描述】:
我已经完成了拍照功能,并且想将拍照的uri传递给另一个组件来查看拍照的照片。如何将 uri 从一个组件传递到另一个组件?
我尝试在父组件中设置状态变量,并在子组件中使用props调用该变量,但出现引用错误。
我在 Parent Component 中声明的状态变量:
state = {
hasCameraPermission: null,
type: Camera.Constants.Type.back,
pho: "",
}
这是父组件中带有动作的按钮:
<MaterialCommunityIcons name="circle-outline"
onPress = { () => this.takePhoto() }
style={{ color: 'white', fontSize: 100
}} />
Parent Component中的拍照功能:
async takePhoto(){
if(this.camera){
const photo = await this.camera.takePictureAsync();
//console.log(photo.uri);
this.setState({formCam: photo.uri});
this.setState({pho: photo.uri});
console.log(this.state.pho);
}
}
最后是我希望在子组件中查看我的图片的 Image 标签:
render(){
return (
<View>
<Image source={{uri: props.pho}}
style={{width:300,
height: 300,
justifyContent: 'center',
alignContent: 'center',
top: 50}} />
</View>
);
}
我希望在父组件中拍摄的照片显示在子组件中。
【问题讨论】:
标签: javascript reactjs react-native expo