Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React UseState Boolean Array Modify One Element

I have a boolean array in useState

const [checkBoxState, setcheckBoxState] = useState([true,true,true,true,true,......]

I need a function that toggle only specific index value A function like this

const HanldeCheck = (index) => {
    setcheckBoxState[index] = !checkBoxState[index];
  };

Please Help

like image 257
Abdullah Mufti Avatar asked Aug 27 '26 15:08

Abdullah Mufti


1 Answers

maybe you can use something like this?

const HanldeCheck = (index) => {
    setcheckBoxState(prevState => prevState.map((item, idx) => idx === index ? !item : item))
};
like image 144
Hovakimyan Avatar answered Aug 30 '26 05:08

Hovakimyan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!