Skip to main content

useToggle

Works as useState but only used for toggling boolean value.

import { useToggle } from "@trakas/react";
function ToggleButton() {
const [value, toggleValue] = useToggle();
return <button onClick={toggleValue}>{value ? "On" : "Off"}</button>;
}

Options#

const [value, toggleValue] = useToggle(initialValue);
  • initialValue: boolean
    • Optional
    • Defaults to false
    • Set this value to change default toggle value

Returns#

  • toggleValue: () => void
    • Switch value from true to false and otherwise
  • value: boolean
    • The last state of toggle value