The initial value of state is false. So, after you do the first click, you set state to !state, which makes it true (because !false equals true). On the second click, it will become false because state was true and setting it to !state made it false (!true equals false). The exclamation mark (!) is read as not.
By the way, I recommend setting the state like this:
setState(prev => !prev);
Hope this helps.
Answer from User456 on Stack OverflowVideos
The initial value of state is false. So, after you do the first click, you set state to !state, which makes it true (because !false equals true). On the second click, it will become false because state was true and setting it to !state made it false (!true equals false). The exclamation mark (!) is read as not.
By the way, I recommend setting the state like this:
setState(prev => !prev);
Hope this helps.
In react when you setState and want to use the previous value as the input you should do this:
setState(pre => !pre)
Here we get the current value by using pre in the useState function. This should work perfectly.
ยป npm install react-switch
ยป npm install react-toggle-button