useToggle

The useToggle hook allows you to manage the Toggle state with less code.

Usage

form.tsx
import { Field, Toggle, useToggle } from '@cerberus/react'
function SomeForm() {
const { checked, handleChange } = useToggle({ checked: 'default' })
return (
<Field>
<Toggle
checked={checked === 'default'}
id="default"
onChange={handleChange}
value="default"
/>
</Field>
)
}

API

interface UseToggleOptions {
checked?: string
onChange?: (e: ChangeEvent<HTMLInputElement>) => void
}
interface ToggleHookReturn {
checked: string
handleChange: (e: ChangeEvent<HTMLInputElement>) => void
}
define function useToggle(options?: UseToggleOptions): ToggleHookReturn

Arguments

The useToggle hook accepts the following optional arguments:

NameDefaultDescription
checkedThe initial checked state of the toggle.
onChangeThe custom handler the hook will call for the onChange event.

Return

The useToggle hook returns a memoized object with the same properties as the options object passed in.

On this page