apps/mantine.dev/src/pages/hooks/use-toggle.mdx
import { UseToggleDemos } from '@docs/demos'; import { Layout } from '@/layout'; import { MDX_DATA } from '@/mdx';
export default Layout(MDX_DATA.useToggle);
The use-toggle hook implements a common state pattern – it switches state between given values:
The hook accepts an array as a single argument; the first option will be used as the default value.
The hook returns an array with the state value and toggle function:
import { useToggle } from '@mantine/hooks';
const [value, toggle] = useToggle(['light', 'dark'] as const);
toggle(); // -> value == 'dark'
toggle(); // -> value == 'light'
// You can force a specific value; in this case, state will be set to the given value
toggle('dark'); // -> value == 'dark'
If you do not provide an array with options, then use-toggle will use boolean values with false as the default:
import { useToggle } from '@mantine/hooks';
const [value, toggle] = useToggle();
// -> value === false
toggle(); // -> value === true
By default, TypeScript will guess your type, but in most cases it's better to use const assertion to prevent type widening:
import { useToggle } from '@mantine/hooks';
useToggle(['light', 'dark']); // value is string
useToggle(['light', 'dark'] as const); // value is 'dark' | 'light'
useToggle<'dark' | 'light'>(['light', 'dark']); // same as above
type UseToggleAction<T> = (value?: React.SetStateAction<T>) => void;
type UseToggleReturnValue<T> = [T, UseToggleAction<T>];
function useToggle<T = boolean>(options?: T[]): UseToggleReturnValue<T>;
The UseToggleReturnValue type is exported from @mantine/hooks;
import type { UseToggleReturnValue } from '@mantine/hooks';