我有一个应用程序正在reactjs和react native中构建。我的状态有一个数组,我正在向其中添加图像 uri。
state = {
images: []
}
问题在于我想将图像 uri 添加到此数组的特定索引处,如下所示
//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})
}
}
但是,上面的方法不起作用,有人可以指出我正确的方向吗?
谢谢
最佳答案
使用Array.splice方法。
this.setState(prevState => {
const { images } = prevState;
images.splice(imageIndex, 0, result.uri);
return images;
})
关于javascript - React - 将项目添加到特定索引处的状态数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51253142/