reactjs - 反应不返回正确的ID
问题描述
我正在尝试用 react 编写一个动态表,但我不明白为什么我不能为按钮功能设置正确的 id。如果我单击按钮,它将返回所有记录的最后一个记录 id,如果我将按钮更改为不使用回调,它将返回正确的 id。
我正在传递需要从父组件返回的内容,如下所示:
const tableContent = [
{
type: 'field',
name: 'id'
},
{
type: 'field',
name: 'name'
},
{
type: 'field',
name: 'website'
},
{
type: 'field',
name: 'telephone'
},
{
type: 'button',
function: test,
variables: ['id'],
param: { variables: { id: '' }},
iconClassName: "trash",
icon: faEdit
}
]
然后在我的表格组件中我这样做:
const tableRows = table.map((record, index) => {
let field = props.content.map((item, index) => {
if (item.type == 'field') {
return (
<td key={index}>{getPropByString(record, item.name)}</td>
)
} else if (item.type == 'button') {
item.param.variables.id = record.id
return (
<td key={index}>
<button onClick={() => item.function(item.param.variables.id)}>test</button>
</td>
)
}
})
return (
<tr key={index}>{field}</tr>
)
})
测试功能是一个简单的控制台日志功能:
const test = (a) => {
console.log(a)
}
解决方案
推荐阅读
- javascript - 带有自定义项目符号的自动图像滑块
- javascript - 模拟内部 axios.create()
- rrdtool - 更新现有 RRD 文件中的值
- algorithm - 合并已排序的 n 个列表
- sql - 在 SQL Server 中将 '2018-07-02 00:01:22.000' 转换为 '02-Jul-18'
- linux - 我如何在 CentOS7 中静态分配 ip
- python - 在我将参数作为python pandas中的dict传递后,read_sql查询返回一个空数据帧
- html - Django REST 可浏览 API 模板更改
- sql - 更改某些列的默认权限
- android - 如何禁用 RecyclerView 单击侦听器但滚动工作