iqaz
iqaz

Reputation: 7

Problem with changing Array State using React Hooks

I am new in React and I have a problem with changing states in Arrays States using Hooks. I have an Array State as u can see below. How I can change only one cell in my state using ,,setFootballersList" without having to write all of the cells"?

const [footballersList, setFootballersList] = useState([["1",true], ["2",true], ["3",true]]);

Thanks for the answers.

Upvotes: 0

Views: 45

Answers (1)

Medi
Medi

Reputation: 1036

You can use the index of the cell in the array. For example pass it to the handler.

const handleStateChange = (targetIndex)=>{
setFootballersList(footballersList.map((footballer, index)=>{
   if(index ===TargetIndex){
     //return updatedFootballer
   }
   return footballer;
}))
}

Upvotes: 1

Related Questions