javascript - ReactJS ..event 处理程序适用于其中一种道具,但不是全部
问题描述
我正在尝试将 id 道具发送到偶数处理程序以处理事件。我有一个地图函数将名称和 id 作为道具发送,如下所示。当我尝试将 id 发送到 Delete Todo 组件中的事件处理程序时,我在 console.log() 中得到未定义的 id。但是当我使用 Name 道具时,console.log() 工作正常。知道我实际上缺少什么。请帮忙。
const todolistt=todolist.map(td=>{
return (
<div>
<Deletetodo todo={td.id} name={td.name} />
</div>
)
})
const handleDelete=(id)=>{
console.log(id)//Getting undefined
}
const Deletetodo = ({id,name}) => {
return (
<div>
<h1>{name}</h1>
<p onClick={()=>{handleDelete(id)}}>X</p>
</div>
)
}
const handleDelete=(name)=>{
console.log(name)//Name appers
}
const Deletetodo = ({id,name}) => {
return (
<div>
<h1>{name}</h1>
<p onClick={()=>{handleDelete(name)}}>X</p>
</div>
)
}
解决方案
您需要像这样命名道具“id”:
<Deletetodo id={td.id} name={td.name} />
您正在传递一个名为“todo”的道具,但正在寻找一个名为“id”的道具
推荐阅读
- php - 调用 php 页面并在该页面上执行 JavaScript
- java - 如何通过在 Java 中恢复数据来设置 int 值
- python - 从数据框列表中查找匹配项。Stupid Backoff 算法的实现
- typescript - 打字稿中的类型安全字典
- git - 带有docker执行器的gitlab-ci:maven无法检索scmbranch,buildScmBranch:未知
- node.js - 如何使用 Windows Server 2019 在 IIS 10 上托管 Node JS 应用程序
- android - 小吃店 - 不能在小吃店设置边距
- javascript - 点击事件页面滚动
- keycloak - 如何在 keycloak 中导入多个领域?
- android - RecyclerView 高度限制