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 Overflow
๐ŸŒ
DhiWise
dhiwise.com โ€บ post โ€บ react-toggle-button-a-journey-from-creation-to-customization
Enhancing User Experience with React Toggle Buttons
October 17, 2023 - A toggle button is a component that can switch between two states, such as on and off, true and false, or selected and deselected. It's a user-friendly way to let users change a setting or attribute without needing to navigate away from the ...
๐ŸŒ
MUI
mui.com โ€บ material-ui โ€บ react-toggle-button
Toggle Button React component - Material UI
The ToggleButtonGroup controls the selected state of its child buttons when given its own value prop.
๐ŸŒ
WebOmnizz
webomnizz.com โ€บ home โ€บ reactjs โ€บ how to create a toggle/switch button in react
How to Create a Toggle/Switch Button in React | WebOmnizz
November 26, 2020 - Letโ€™s add some global settings to make our toggle button component extendable like disable the toggle button, change the default value, change icons, callback event on state change, additional class name etc. ... Now, go to the bottom of the page and start adding the property types to ToggleButton component. import React, { useState } from 'react'; import PropTypes from 'prop-types'; const ToggleButton = () => { const [toggle, setToggle] = useState(false); const triggerToggle = () => { setToggle( !toggle ) } return( <div onChange={triggerToggle} className={`wrg-toggle ${toggle ?
๐ŸŒ
Medium
medium.com โ€บ @nishaadequate123 โ€บ toggle-button-in-react-js-technical-chamber-644475687eb0
Toggle Button in React JS |Technical Chamber | by Nishaadequate | Medium
July 4, 2023 - The handleToggle function is triggered when the button is clicked. It updates the value of isToggled by toggling it between true and false using the setToggle function.
๐ŸŒ
Gdowens
gdowens.github.io โ€บ react-toggle-button
React Toggle Button
We cannot provide a description for this page right now
๐ŸŒ
C# Corner
c-sharpcorner.com โ€บ article โ€บ how-to-create-a-toggle-switch-in-react
How To Create A Toggle Switch In React
November 25, 2022 - Create a folder for toggle switch and inside it create a component, 'toggleSwitch.js'. Add the following code to this component. import React, { useState } from 'react'; import ReactSwitch from 'react-switch'; function ToggleSwitch() { const [checked, setChecked] = useState(true); const handleChange = val => { setChecked(val) } return ( <div className="app" style={{textAlign: "center"}}> <h4>Toggle switch in React</h4> <ReactSwitch checked={checked} onChange={handleChange} /> </div> ); } export default ToggleSwitch;
๐ŸŒ
Base UI
base-ui.com โ€บ react โ€บ components โ€บ toggle
Toggle ยท Base UI
'use client'; import * as React from 'react'; import { Toggle } from '@base-ui/react/toggle'; import styles from './index.module.css'; export default function ExampleToggle() { return ( <div className={styles.Panel}> <Toggle aria-label="Favorite" className={styles.Button} render={(props, state) => { if (state.pressed) { return ( <button type="button" {...props}> <HeartFilledIcon className={styles.Icon} /> </button> ); } return ( <button type="button" {...props}> <HeartOutlineIcon className={styles.Icon} /> </button> ); }} /> </div> ); } function HeartFilledIcon(props: React.ComponentProps<'svg
Find elsewhere
๐ŸŒ
PrimeReact
primereact.org โ€บ togglebutton
PrimeReact | React UI Component Library
ToggleButton is used to select a boolean value using a button.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-use-the-toggle-component-in-react
How to use the toggle component in React
A toggle component in React, such as a toggle button, acts like a switch that, once clicked, will show or hide an element or a group of elements. In this Answer, weโ€™ll create a React toggle button from scratch.
๐ŸŒ
npm
npmjs.com โ€บ package โ€บ react-switch
react-switch - npm
December 14, 2024 - A draggable toggle-switch component for React.
      ยป npm install react-switch
    
Published ย  Dec 14, 2024
Version ย  7.1.0
๐ŸŒ
Adobe React Spectrum
react-spectrum.adobe.com โ€บ react-aria โ€บ ToggleButton.html
ToggleButton โ€“ React Aria - React Spectrum Libraries
A toggle button allows a user to toggle a selection on or off, for example switching between two states or modes. ... This sets the --tint CSS variable used by the Vanilla CSS examples.
๐ŸŒ
Amplify UI
ui.docs.amplify.aws โ€บ react โ€บ components โ€บ togglebutton
ToggleButton | Amplify UI for React
To control the selected state of its child buttons, both value and onChange props must be provided. ... import * as React from 'react'; import { MdFormatBold, MdFormatColorFill, MdFormatItalic, MdFormatUnderlined, } from 'react-icons/md'; import { ToggleButton, ToggleButtonGroup } from '@aws-amplify/ui-react'; export const DefaultToggleButtonGroupExample = () => { const [multipleValue, setMultipleValue] = React.useState(['bold']); return ( <ToggleButtonGroup value={multipleValue} onChange={(value) => setMultipleValue(value as string[])} > <ToggleButton value="bold"> <MdFormatBold /> </ToggleButton> <ToggleButton value="italic"> <MdFormatItalic /> </ToggleButton> <ToggleButton value="underlined"> <MdFormatUnderlined /> </ToggleButton> <ToggleButton value="color-fill"> <MdFormatColorFill /> </ToggleButton> </ToggleButtonGroup> ); };
๐ŸŒ
npm
npmjs.com โ€บ package โ€บ react-toggle-button
react-toggle-button - npm
November 15, 2017 - A React Component.. Latest version: 2.2.0, last published: 8 years ago. Start using react-toggle-button in your project by running `npm i react-toggle-button`. There are 22 other projects in the npm registry using react-toggle-button.
      ยป npm install react-toggle-button
    
Published ย  Nov 15, 2017
Version ย  2.2.0
๐ŸŒ
SitePoint
sitepoint.com โ€บ blog โ€บ javascript โ€บ create a toggle switch in react as a reusable component
Create a Toggle Switch in React as a Reusable Component โ€” SitePoint
November 15, 2024 - Learn how to create an iOS-inspired toggle switch using React components, building a simple demo React App for using this custom toggle switch component.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ reactjs โ€บ how-to-create-a-toggle-switch-in-react-as-a-reusable-component
How to Create a Toggle Switch in React as a Reusable Component ? - GeeksforGeeks
July 23, 2025 - First, Create a ToggleSwitch Component with a label prop. Define an input of type checkbox inside the component. Style the Switch using CSS :before, :after selectors. Add a label for the input and export the component to get the required output.
๐ŸŒ
Syncfusion
syncfusion.com โ€บ react โ€บ toggle switch button
React Toggle Switch Button โ€“ Responsive & Customizable | Syncfusion
February 3, 2026 - The React Toggle Switch Button component is a custom HTML5 input-type checkbox component that allows you to perform a toggle (on/off) action between checked and unchecked states.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ toggle-elements-in-react-using-hooks
How to Toggle an Element in React using React Hooks
April 16, 2025 - Then we create two variables called toggle and setToggle, while setting the initial state to true. Next, inside of the jsx section, we create a button that has an onClick event handler.