I have a list where I'm iterating though every item and it has a checkbox to select a particular list item. The problem I'm facing now is, when I select one item, it checks all the items in the list. How do I check only particular item that is selected?
Excerpt from my code
export default function App() {
const [checked, setChecked] = useState(false);
const names = ["John", "Doe", "Jim"];
const handleCheck = (event, index) => {
console.log(event.target.checked);
console.log(index);
setChecked(event.target.checked);
return event.target.checked;
};
return (
<div className="App">
{names.map((values, index) => (
<div>
<input
type="checkbox"
checked={checked}
onChange={(event) => handleCheck(event, index)}
/>
{values}
</div>
))}
</div>
);
}
I created a working example using CodeSandbox. Could anyone please help?