javascript - 如何使用 onClick 从 JSX 组件向反应函数组件传递参数
问题描述
这是我的大代码中的一个非常小的片段:
import React from "react";
const Home = () => {
return (
imgFilter.map((imgs) => {
return ( <
Col sm = "3"
xs = "12"
key = {
imgs.id
}
className = "trend-image" >
<
img src = {
imgs.path
}
alt = {
imgs.id
}
className = "img-fluid"
onClick = {
Price
}
/> <
/Col>
)
});
}
在这段代码中,您可以在一行中看到它是这样写的onClick={Price}
。这里的 Price 是我正在导入的功能组件。现在,普莱斯旨在接受争论。在这里,我想在imgs.id
单击图像时传递价格。我该怎么做
解决方案
做一个箭头函数并Price
调用imgs.id
import React from "react";
const Home = () => {
return (
imgFilter.map((imgs) => {
return ( <
Col sm = "3"
xs = "12"
key = {
imgs.id
}
className = "trend-image" >
<
img src = {
imgs.path
}
alt = {
imgs.id
}
className = "img-fluid"
onClick = {() => Price(imgs.id)}
/> <
/Col>
)
});
}
推荐阅读
- couchbase - Couchbase 双嵌套数组中每个项目的子查询
- react-native - 如何根据条件在 ScrollView 中换行或包装内容
- java - 如何在 Java 中从 CallScreeningService 开始活动
- php - 非法字符串偏移 'name' CodeIgniter 4
- nlp - Gensim 的 word2vec 从 epoch 1 开始有 0 的损失?
- android - Android:完成所有活动并将用户带回家而不加载主页
- swift - 检查标签/页面是否固定在 Safari 应用程序扩展中
- javascript - 如何使用 rsuite - React Suite 范围滑块?
- javascript - 将动态创建的列表项 ID 绑定到 jQuery 插件
- sas - 在sas中将多条记录压缩为一条记录