admin管理员组文章数量:1327974
I have an app im building in reactjs and react native. I have an array in my state that I'm adding image uri's to.
state = {
images: []
}
The problem lies in that I want to add images uris to this array at a specific index like this
//pass the index I want the image uri assigned to
pickImage = (imageIndex) => {
let result = await ImagePicker.launchImageLibraryAsync({
allowsEditing: true,
aspect: [4, 3],
});
if (!result.cancelled) {
this.setState({images[imageIndex]: result.uri})
}
}
however, the above doesn't work, can someone point me in the right direction?
Thanks
I have an app im building in reactjs and react native. I have an array in my state that I'm adding image uri's to.
state = {
images: []
}
The problem lies in that I want to add images uris to this array at a specific index like this
//pass the index I want the image uri assigned to
pickImage = (imageIndex) => {
let result = await ImagePicker.launchImageLibraryAsync({
allowsEditing: true,
aspect: [4, 3],
});
if (!result.cancelled) {
this.setState({images[imageIndex]: result.uri})
}
}
however, the above doesn't work, can someone point me in the right direction?
Thanks
Share Improve this question asked Jul 9, 2018 at 19:58 juicy89juicy89 4585 silver badges14 bronze badges 4- Can you explain in what way it doesn't work? – Colin Ricardo Commented Jul 9, 2018 at 19:59
- It actually throws a syntax error with unexpected token , – juicy89 Commented Jul 9, 2018 at 20:01
-
@juicy89 Yes
{images[imageIndex]: result.uri}
is not a valid object literal. – Arup Rakshit Commented Jul 9, 2018 at 20:04 - Both your answers worked perfectly as expected. Without sounding ridiculous, is one preferred over the other in a general sense? I only ask as I'm still relatively new to JS – juicy89 Commented Jul 9, 2018 at 20:11
3 Answers
Reset to default 5Use Array.splice method.
this.setState(prevState => {
const { images } = prevState;
images.splice(imageIndex, 0, result.uri);
return images;
})
It looks like you're trying to modify state somewhat directly. Try this instead:
pickImage = (imageIndex) => {
let result = await ImagePicker.launchImageLibraryAsync({
allowsEditing: true,
aspect: [4, 3],
});
const images = { ...this.state };
images[imageIndex] = result.uri;
if (!result.cancelled) {
this.setState({ images })
}
A new array should be created with a different value in that index. We want to make a new array because it is an antipattern to mutate state. The new array can be created by making a copy of this.state.images and modifying the value at the specific index.
const copyOfImages = [...this.state.images];
copyOfImages[imageIndex] = newValue;
this.setState({images: copyOfImages});
Array.prototype.map can be leveraged to make this code a bit more functional.
this.setState(previousState => ({
images: previousState.images.map(
(image, index) => index === imageIndex ? result.uri : image
);
});
本文标签: javascriptreactadding item to array in state at a specific indexStack Overflow
版权声明:本文标题:javascript - react - adding item to array in state at a specific index - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742253027a2441115.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论