React Usestate Boolean Array Modify One Element

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

1 Answer

maybe you can use something like this?

const HanldeCheck = (index) => {
    setcheckBoxState(prevState => prevState.map((item, idx) => idx === index ? !item : item))
};
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett
Author

Chloe Bennett

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.