Answer the question
In order to leave comments, you need to log in
How to update multiple state values using one function?
Good afternoon ! I have a state like this:
state = {
photos : {
driverPhoto : null,
passportPhoto : null,
driverLicensePhoto: null,
},
};
useLibraryHandler = async () => {
await this.askPermissionsAsync();
let result = await ImagePicker.launchImageLibraryAsync({
allowsEditing: true,
aspect: [4, 3]
});
this.setState({ photos : { ...this.state.photos, driverPhoto : result.uri } });
};
<TouchableHighlight onPress={this.useLibraryHandler}></TouchableHighlight>
<TouchableHighlight onPress={this.useLibraryHandler('driverPhoto')}></TouchableHighlight>
Answer the question
In order to leave comments, you need to log in
So pass the name of a specific element as a separate prop:
<TouchableHighlight onPress={this.useLibraryHandler} photoType="driverPhoto" />
No closures will be needed, just read the passed string.let newPhotos = {...this.state.photos};
newPhotos[this.props.photoType] = result.uri;
this.setState({ photos : newPhotos });
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question