如何使用 React native 获取作为数组项的单击图像的索引
How to get index of the clicked images which is an item of array using React native
我制作了一个数组来从用户那里获取 5 张图片。我需要为用户提供 select 的功能,并动态地从该数组中删除图像。我目前正在使用 splice() 方法来进行操作。但是当我选择要删除的图像时..它正在删除整个图像 onPress
renderImages = () => {
let image = [];
this.state.image.slice(0, 5).map((item, index) => {
image.push(
<View key={index} style={{ padding: 16 }}>
<Image source={{ uri: item }} style={{ width: 60, height: 60 }} />
<Icon
name="window-close"
size={15}
color="#d3d3d3"
style={{ position: "absolute", top: 5, right: 5 }}
onPress={index => {
this.setState({ image: this.state.image.splice(index, 1) });
}}
/>
</View>
);
});
return image;
};
首先不要直接改变状态,更多关于这个 . splice 不会重新调整更新的数组而是 returns 删除元素的数组。
renderImages = () => {
let imagesToDisplay = [];
const allImages = this.state.image;
allImages.slice(0, 5).map((item, index) => {
imagesToDisplay.push(
<View key={index} style={{ padding: 16 }}>
<Image source={{ uri: item }} style={{ width: 60, height: 60 }} />
<Icon
name="window-close"
size={15}
color="#d3d3d3"
style={{ position: "absolute", top: 5, right: 5 }}
onPress={index => {
const image = this.state.image;
image.splice(index, 1);
this.setState({ image });
}}
/>
</View>
);
});
return imagesToDisplay;
};
这里的问题是您使用 splice
直接对状态进行了修改。您需要先克隆状态:
renderImages = () => {
let image = [];
this.state.image.slice(0, 5).map((item, index) => {
image.push(
<View key={index} style={{ padding: 16 }}>
<Image source={{ uri: item }} style={{ width: 60, height: 60 }} />
<Icon
name="window-close"
size={15}
color="#d3d3d3"
style={{ position: "absolute", top: 5, right: 5 }}
onPress={index => {
let images = [...this.state.image]
images.splice(index, 1)
this.setState({ image: images });
}}
/>
</View>
);
});
return image;
};
我制作了一个数组来从用户那里获取 5 张图片。我需要为用户提供 select 的功能,并动态地从该数组中删除图像。我目前正在使用 splice() 方法来进行操作。但是当我选择要删除的图像时..它正在删除整个图像 onPress
renderImages = () => {
let image = [];
this.state.image.slice(0, 5).map((item, index) => {
image.push(
<View key={index} style={{ padding: 16 }}>
<Image source={{ uri: item }} style={{ width: 60, height: 60 }} />
<Icon
name="window-close"
size={15}
color="#d3d3d3"
style={{ position: "absolute", top: 5, right: 5 }}
onPress={index => {
this.setState({ image: this.state.image.splice(index, 1) });
}}
/>
</View>
);
});
return image;
};
首先不要直接改变状态,更多关于这个
renderImages = () => {
let imagesToDisplay = [];
const allImages = this.state.image;
allImages.slice(0, 5).map((item, index) => {
imagesToDisplay.push(
<View key={index} style={{ padding: 16 }}>
<Image source={{ uri: item }} style={{ width: 60, height: 60 }} />
<Icon
name="window-close"
size={15}
color="#d3d3d3"
style={{ position: "absolute", top: 5, right: 5 }}
onPress={index => {
const image = this.state.image;
image.splice(index, 1);
this.setState({ image });
}}
/>
</View>
);
});
return imagesToDisplay;
};
这里的问题是您使用 splice
直接对状态进行了修改。您需要先克隆状态:
renderImages = () => {
let image = [];
this.state.image.slice(0, 5).map((item, index) => {
image.push(
<View key={index} style={{ padding: 16 }}>
<Image source={{ uri: item }} style={{ width: 60, height: 60 }} />
<Icon
name="window-close"
size={15}
color="#d3d3d3"
style={{ position: "absolute", top: 5, right: 5 }}
onPress={index => {
let images = [...this.state.image]
images.splice(index, 1)
this.setState({ image: images });
}}
/>
</View>
);
});
return image;
};