On check box checked i want to display only same row delete button instead of all delete button.
On check box checked i want to display only same row delete button instead of all delete button.
I have written code to delete the concern of checkbox row. Delete button is working, But when i checked
checkbox all delete buttons displaying instead of one delete button.
// Reactjs
const games = ['cricket', 'vollyboll', 'vollyball'];
const [game, setGame] = useState(games);
const [myCheck, setMyCheck] = useState(false)
const deleteItem = (itemindex)=>{
let removeGame = game.filter((ele, i)=>{
return i!==itemindex
});
setGame(removeGame)
}
// const checkedBoxes = (e) =>{
// const {myCheck, isChecked} = e.target;
// if(myCheck === isChecked){
// return <button onClick={()=>deleteItem(i)}>Delete</button>
// }
// }
return (
<div className="App">
<h3>Play Games</h3>
{
game.map((ele, i)=>{
return(
<div key={i}>
<li>
<input type="checkbox" value={myCheck} onChange={(e)=>setMyCheck(true)}/> {ele}
{
myCheck ? <button onClick={()=>deleteItem(i)}>Delete</button>: null
}
</li>
</div>
)
})
}
</div>
);
}
On check box checked i want to display only same row delete button instead of all delete button.
2
Answers
Instead of using a boolean for
myCheck
, you can use the index of the button, kinda the same idea as you use in the remove function.Then you can alter the check to
myCheck === i
to only show the button next to the matching checkboxThis will do the work