0

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.

2
  • Please share the desired output. I am not getting what you want to do. What is cell that you are referring Commented Apr 16, 2021 at 10:45
  • I give u an example. I have a function ,,changeFootbalers(x)" which should work f.e: for changeFootballers(0) changing state from [["1",true], ["2",true], ["3",true] to [["1",false], ["2",true], ["3",true] Commented Apr 16, 2021 at 10:47

1 Answer 1

1

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;
}))
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.