Skip to content
+

Switch

Switches are UI elements that let users choose between two states—most commonly on/off.

useSwitch API

Import

import { useSwitch } from '@mui/base/useSwitch';
// or
import { useSwitch } from '@mui/base';
Learn about the difference by reading this guide on minimizing bundle size.

Parameters

NameTypeDefaultDescription
checkedboolean-

If true, the switch is checked.

defaultCheckedboolean-

The default checked state. Use when the component is not controlled.

disabledbooleanfalse

If true, the component is disabled and can't be interacted with.

inputRefReact.Ref<HTMLInputElement>-

Ref to the underlying input element.

namestring-

Name of the underlying input element.

onChangeReact.ChangeEventHandler<HTMLInputElement>-

Callback fired when the state is changed.

readOnlybooleanfalse

If true, the component is read-only. Functionally, this is equivalent to being disabled, but the assistive technologies will announce this differently.

requiredbooleanfalse

If true, the switch must be checked for the browser validation to pass.

Return value

NameTypeDefaultDescription
checkedboolean-

If true, the component will be checked.

getButtonProps(externalProps?: React.ComponentPropsWithRef<'button'>) => React.ComponentPropsWithRef<'button'>-

Resolver for the button element's props.

getInputProps(externalProps?: React.ComponentPropsWithRef<'input'>) => React.ComponentPropsWithRef<'input'>-

Resolver for the input element's props.