Function useToggle

  • Provides a boolean toggle that does not persist between page reloads.

    Parameters

    • OptionalinitialValue: boolean = false

      The initial value of the toggle.

    Returns [boolean, (() => void)]

    The current state of the toggle.

    const DemoUseToggle = () => {
    const [state, toggle] = useToggle();
    return (
    <>
    <h1>
    My favorite color is: {state ? "green" : "red"}
    </h1>
    <button onClick={toggle}>Change Opinion</button>
    </>
    );
    };