配置文件.js
Profile 屏幕使用 redux 从 WebApi 获取其数据,并根据 imageUrl 显示图像。SelectImage() 调用函数让用户打开图库并选择新图像。
选择图像后,我希望图像显示在屏幕上并替换this.props.items.imageUrl
. 我刚刚启动了 redux 并将其实现到我的应用程序中。我想更新 redux 的状态并在其他屏幕上访问,因为图片也会显示在其他屏幕上。但是,我无法更改应用程序内的图像。
所有数据都在items里面
<TouchableOpacity
onPress={() => this.selectImage()}>
<View style={styles.piccontainer}>
<Image
source={{ uri: this.props.items.imageUrl }}
style={styles.photo} />
</View>
</TouchableOpacity>
<TouchableOpacity style={styles.button}
onPress={() => {
this.uploadImage();
this.goBack()
}}>
<View>
<Text style={styles.text}>Save</Text>
</View>
</TouchableOpacity>
这是我的 selectImage() 函数
selectImage = async () => {
let result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.All,
allowsEditing: true,
aspect: [4, 3],
quality: 1
});
let file = result.uri
if (!result.cancelled) {
this.updatePicture(file)
}
}
我相信我必须mapDispatchToProps
采取行动。
function mapDispatchToProps(dispatch) {
return {
updatePicture: file => {dispatch(updatePicture(file))}
}
}
调用该函数后我迷路了,我是否将减速器和动作放在与获取配置文件数据的函数相同的位置?我是否创建了一个新文件,但如果我这样做了,我将初始状态设置为什么?感谢您的帮助。我很感激。
profileReducer.js
const initialState = {
profilepic: '',
};
export default function updateprofileReducer(state = initialState, action) {
switch (action.type) {
case UPDATE_PICTURE:
return {
...state,
profilepic: action.payload.file
}
default:
return state;
}
}
profileAction.js
export const updatePicture = file => ({
type: UPDATE_PICTURE,
payload: file
})