英文:
Upon Click on button i want that particular div to trigger event
问题
有一个待办事项应用程序。
每当我点击复选框时,它应该只触发Div数组元素的handleCheck()函数。
它在所有的Div数组元素中触发了handleCheck()。
const todoData = [
{id:0,todoname:"学习",todotoday:"今天完成CSS"},
{id:1,todoname:"编程",todotoday:"Leetcode解决2个问题"}
];
const [myArray,setmyArray] = useState(todoData);
const [isChecked, setIsChecked] = useState();
const handleCheck = (id) =>{
console.log(id);
setIsChecked(current => !current);
}
return (
<div className="todomain">
{
myArray.map((obj)=>{
return ( <div className="todobox" key={obj.id} style={{opacity:isChecked ? 0 : ''}}>
<div className="checkcont">
<img src={check} alt="Check" className='chkbtn btn' onClick={() => handleCheck(obj.id)}/>
</div>
<h2 className="head" >待办事项:{obj.todoname}</h2>
<h3 className="todocont">{obj.todotoday}</h3>
<div className="todoboxbtn">
<TodoIcon />
</div>
</div> )
})
}
</div>
)
英文:
Have a Todo App.
Whenever i will click on check it should only trigger handleCheck() for that particular element of Div(Array Elements)
It is triggering handleCheck() in all the div(Array Elements).
const todoData = [
{id:0,todoname:"Study",todotoday:"Completing CSS today"},
{id:1,todoname:"Coding",todotoday:"Leetcode 2 Problems"}
];
const [myArray,setmyArray] = useState(todoData);
const [isChecked, setIsChecked] = useState();
const handleCheck = (id) =>{
console.log(id);
setIsChecked(current => !current);
}
return (
<div className="todomain">
{
myArray.map((obj)=>{
return ( <div className="todobox" key={obj.id} style={{opacity:isChecked ? 0 : ''}}>
<div className="checkcont">
<img src={check} alt="Check" className='chkbtn btn' onClick={() => handleCheck(obj.id)}/>
</div>
<h2 className="head" >Todo: {obj.todoname}</h2>
<h3 className="todocont">{obj.todotoday}</h3>
<div className="todoboxbtn">
<TodoIcon />
</div>
</div> )
})
}
</div>
)
答案1
得分: 1
为了使这段代码正常工作,你应该在你的数据中添加一个 isChecked
属性,并修改更新状态的方式。
你的最终代码将如下所示:
const todoData = [
{
id: 0,
todoname: '学习',
todotoday: '今天完成 CSS',
isChecked: false, // 新的布尔属性
},
{
id: 1,
todoname: '编码',
todotoday: 'Leetcode 解决 2 个问题',
isChecked: false, // 新的布尔属性
},
]
const [myArray, setMyArray] = useState(todoData)
/**
* 给定对象的 id,映射 MyArray 中的所有项目。如果找到 obj.id,则更新其 isChecked 属性。
* 最后使用 setMyArray 更新状态并重新渲染 UI。
* @param {Number} id MyArray 中某个对象的 id
*/
const toggleTodoCheck = (id) => {
const updatedMyArray = myArray.map((obj) => {
if (obj.id === id) return { ...obj, isChecked: !obj.isChecked }
return obj
})
setMyArray(updatedMyArray)
}
return (
<div className="todomain">
{myArray.map((obj) => {
return (
<div
className="todobox"
key={obj.id}
style={{ opacity: obj.isChecked ? 0 : '' }}
>
<div className="checkcont">
<img
src={check}
alt="Check"
className="chkbtn btn"
onClick={() => toggleTodoCheck(obj.id)}
/>
</div>
<h2 className="head">待办事项:{obj.todoname}</h2>
<h3 className="todocont">{obj.todotoday}</h3>
<div className="todoboxbtn">
<TodoIcon />
</div>
</div>
)
})}
</div>
)
英文:
In order to make this code work properly you should add isChecked
property to your data and modify the way you update your state.
You final code will look like this:
const todoData = [
{
id: 0,
todoname: 'Study',
todotoday: 'Completing CSS today',
isChecked: false, // new boolean property
},
{
id: 1,
todoname: 'Coding',
todotoday: 'Leetcode 2 Problems',
isChecked: false, // new boolean property
},
]
const [myArray, setMyArray] = useState(todoData)
/**
* Given obj id, map all items inside MyArray. If obj.id was found, update it's
* isChecked property. Finally use setMyArray to update the state and re-render UI
* @param {Number} id the id of a certain obj in MyArray
*/
const toggleTodoCheck = (id) => {
const updatedMyArray = myArray.map((obj) => {
if (obj.id === id) return { ...obj, isChecked: !obj.isChecked }
return obj
})
setMyArray(updatedMyArray)
}
return (
<div className="todomain">
{myArray.map((obj) => {
return (
<div
className="todobox"
key={obj.id}
style={{ opacity: obj.isChecked ? 0 : '' }}
>
<div className="checkcont">
<img
src={check}
alt="Check"
className="chkbtn btn"
onClick={() => toggleTodoCheck(obj.id)}
/>
</div>
<h2 className="head">Todo: {obj.todoname}</h2>
<h3 className="todocont">{obj.todotoday}</h3>
<div className="todoboxbtn">
<TodoIcon />
</div>
</div>
)
})}
</div>
)
答案2
得分: 0
因为 "一件事" 和 "许多事情" 之间存在差异。
这个状态表示许多事情(一个数组):
const [myArray, setmyArray] = useState(todoData);
但这个状态表示一个事情(一个单一的值):
const [isChecked, setIsChecked] = useState();
因此,当 isChecked
为 true
时,它应用于 myArray
中的哪个元素?目前您的选项是 "所有元素" 或 "没有元素"。
也许您并不只想存储一个布尔值,也许您想存储 id
值?例如:
const [checkedItem, setCheckedItem] = useState();
const handleCheck = (id) => {
console.log(id);
setCheckedItem(id);
}
然后 checkedItem
将包含当前选中的 "项" 的 id
。然后,您可以在代码中比较该 id
以有条件地为元素添加样式:
<div
className="todobox"
key={obj.id}
style={{ opacity: (checkedItem === obj.id) ? 0 : '' }}
>
或者,如果您想要追踪 "已选中的项" 作为 "许多事情",那么这将是一个数组。例如:
const [checkedItems, setCheckedItems] = useState([]);
勾选/取消勾选项将涉及在该数组中添加/删除项。例如:
const handleCheck = (id) => {
console.log(id);
if (checkedItems.includes(id)) {
setCheckedItems(checkedItems.filter(i => i !== id));
} else {
setCheckedItems([...checkedItems, id]);
}
}
并且 JSX 标记中的条件将检查该数组是否 包含 obj.id
值(而不是 是 obj.id
值)。例如:
<div
className="todobox"
key={obj.id}
style={{ opacity: checkedItems.includes(obj.id) ? 0 : '' }}
>
英文:
Because there's a difference between "one thing" and "many things".
This state represents many things (an array):
const [myArray,setmyArray] = useState(todoData);
But this state represents one thing (a single value):
const [isChecked, setIsChecked] = useState();
So, when isChecked
is true
, which element of myArray
does it apply to? Currently your options are "all of them" or "none of them".
Instead of just storing a boolean value, perhaps you meant to store the id
value? For example:
const [checkedItem, setCheckedItem] = useState();
const handleCheck = (id) =>{
console.log(id);
setCheckedItem(id);
}
Then checkedItem
would contain the id
of the one "item" which is currently checked. You'd then compare that id
in the code to conditionally style your elements:
<div
className="todobox"
key={obj.id}
style={{ opacity: (checkedItem === obj.id) ? 0 : '' }}
>
Alternatively, if you want to track "checked items" as "many things" then that would be an array. For example:
const [checkedItems, setCheckedItems] = useState([]);
Checking/unchecking items would be a matter of adding/removing items in that array. For example:
const handleCheck = (id) =>{
console.log(id);
if (checkedItems.includes(id)) {
setCheckedItems(checkedItems.filter(i => i !== id));
} else {
setCheckedItems([...checkedItems, id]);
}
}
And the condition in the JSX markup would be checking if that array contains the obj.id
value (rather than is the obj.id
value). For example:
<div
className="todobox"
key={obj.id}
style={{ opacity: checkedItems.includes(obj.id) ? 0 : '' }}
>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论