javascript - 唯一地设置元素上的属性
问题描述
我正在制作一个反应原生的应用程序。在我的应用程序中,我正在获取一些 json 并使用循环,我迭代 json 数组,然后在循环中创建视图。现在有一个问题,我在循环中创建的视图中使用了onpress()
函数。现在有一个问题,例如我有 3 个视图,每个视图都有一个按钮。现在,当我按下按钮时,我想替换按钮单击时的图像,所有 3 个视图的图像都已更改,但我只想更改单击按钮的图像
const imagesPath = {
minus: require('./heartFill.png'),
plus: require('./heart.png')
}
LikeImage = () =>
{
this.setState(state => ({ open: !state.open }));
const ref=this.heading.current.props;
const refpic=this.pic.current;
alert(ref);
}
const imageName = this.getImageName();
let data = [];
const rendercard = () => {
for (let item in this.state.jsons) {
let Image_Http_URL = { uri: this.state.jsons[item].url };
const piced = this.state.jsons[item].url;
data.push(
<Card
key={generateKey(piced)}
style={{
elevation: 7,
borderBottomEndRadius: 20,
borderBottomStartRadius: 20,
borderTopEndRadius: 20,
borderTopStartRadius: 20,
marginBottom: 20,
backgroundColor: "#000000",
height: 350,
padding: 0
}}
>
<Card.Content
style={{
paddingLeft: 0,
paddingRight: 0,
paddingTop: 0,
paddingBottom: 0,
borderTopEndRadius: 20,
borderTopStartRadius: 20
}}
>
<Image
ref={this.pic}
style={{
borderTopLeftRadius: 30,
borderTopRightRadius: 30,
alignItems: "center",
justifyContent: "center",
width: "100%",
height: "85%"
}}
resizeMode="stretch"
source={Image_Http_URL}
></Image>
<Text
ref={this.heading}
style={{
fontSize: 25,
fontWeight: "bold",
color: "white",
paddingStart: 10,
paddingEnd: 10,
position: "absolute",
bottom: 150
}}
>
“ {this.state.jsons[item].quotes} ”
</Text>
<View
style={{
alignItems: "center",
justifyContent: "space-between",
flexDirection: "row",
backgroundColor: "white",
width: "100%",
height: "15%",
borderBottomEndRadius: 20,
borderBottomStartRadius: 20
}}
>
<View
style={{
flexDirection: "row",
alignItems: "center",
justifyContent: "center"
}}
>
<ImageBackground
onPress={this.LikeImage}
style={{ width: 30, height: 30, marginStart: 20 }}
source={imagesPath[imageName]}
></ImageBackground>
<Text
onPress={this.LikeImage}
style={{ fontSize: 15, fontWeight: "bold" }}
>
{" "}
Like
</Text>
</View>
<View
style={{
flexDirection: "row",
alignItems: "center",
justifyContent: "center"
}}
>
<Image
style={{ width: 30, height: 30 }}
source={require("./download.png")}
></Image>
<Text style={{ fontSize: 15, fontWeight: "bold" }}> Save</Text>
</View>
<View
style={{
flexDirection: "row",
alignItems: "center",
justifyContent: "center"
}}
>
<Image
style={{ width: 30, height: 30 }}
source={require("./share.png")}
></Image>
<Text style={{ fontSize: 15, fontWeight: "bold", marginEnd: 20 }}>
{" "}
Share
</Text>
</View>
</View>
</Card.Content>
</Card>
);
}
return data;
};
解决方案
推荐阅读
- jquery - 绘制折线图中的动态数据问题
- windows-subsystem-for-linux - 无法在 WSL 中使用任何 Windows 驱动器
- asynchronous - websocket 意外关闭
- typescript - 如何根据事件名称缩小回调事件类型
- c# - 用 \n 替换特殊字符,但将文本保持在同一“列”中
- azure - 尝试将托管标识与 Azure 服务总线一起使用
- pdf - Tesseract:线检测太敏感
- html - 容器边框和子元素背景之间的小间隙(CHROME)
- python - 从 python 中的数据帧导出到 MS 访问时的编码问题
- python - 在python中,我们如何执行条件操作列表,其中列表的项目或元素是关键字