Skip to content

Switch Primitive

Types Primitives Slot Primitives

@radix-ui/react-switch


A control enabling the user to switch between a checked and unchecked state.

Installation

Install @radix-ui/react-switch

Terminal window
npx expo install @radix-ui/react-switch

Copy/paste the following code for web to ~/components/primitives/switch/switch.web.tsx

Copy/paste the following code for native to ~/components/primitives/switch/switch.tsx

Copy/paste the following code for types to ~/components/primitives/switch/types.ts

Copy/paste the following code for exporting to ~/components/primitives/switch/index.ts

Usage

import * as React from 'react';
import * as SwitchPrimitive from '~/components/primitives/switch';
export default function SwitchScreen() {
const [checked, setChecked] = React.useState(false);
return (
<SwitchPrimitive.Root checked={checked} onCheckedChange={setChecked}>
<SwitchPrimitive.Thumb />
</SwitchPrimitive.Root>
);
}

Props

Root

Extends Pressable props

PropTypeNote
checkedboolean
onCheckedChange(checked: boolean) => void
disabledboolean(optional)
asChildboolean(optional)
onKeyDown(ev: React.KeyboardEvent) => voidWeb Only (optional)

Thumb

Extends View props

PropTypeNote
asChildboolean(optional)