logoAnt Design

⌘ K
  • Design
  • Development
  • Components
  • Blog
  • Resources
5.25.0
  • Components Overview
  • General
    • Button
    • FloatButton
      5.0.0
    • Icon
    • Typography
  • Layout
    • Divider
    • Flex
      5.10.0
    • Grid
    • Layout
    • Space
    • Splitter
      5.21.0
  • Navigation
    • Anchor
    • Breadcrumb
    • Dropdown
    • Menu
    • Pagination
    • Steps
    • Tabs
  • Data Entry
    • AutoComplete
    • Cascader
    • Checkbox
    • ColorPicker
      5.5.0
    • DatePicker
    • Form
    • Input
    • InputNumber
    • Mentions
    • Radio
    • Rate
    • Select
    • Slider
    • Switch
    • TimePicker
    • Transfer
    • TreeSelect
    • Upload
  • Data Display
    • Avatar
    • Badge
    • Calendar
    • Card
    • Carousel
    • Collapse
    • Descriptions
    • Empty
    • Image
    • List
    • Popover
    • QRCode
      5.1.0
    • Segmented
    • Statistic
    • Table
    • Tag
    • Timeline
    • Tooltip
    • Tour
      5.0.0
    • Tree
  • Feedback
    • Alert
    • Drawer
    • Message
    • Modal
    • Notification
    • Popconfirm
    • Progress
    • Result
    • Skeleton
    • Spin
    • Watermark
      5.1.0
  • Other
    • Affix
    • App
      5.1.0
    • ConfigProvider
    • Util
      5.13.0

Switch

Used to toggle between two states.
Importimport { Switch } from "antd";
Sourcecomponents/switch
Docs
Edit this pageChangelog

Resources

Ant Design X
Ant Design Charts
Ant Design Pro
Pro Components
Ant Design Mobile
Ant Design Mini
Ant Design Web3
Ant Design Landing-Landing Templates
Scaffolds-Scaffold Market
Umi-React Application Framework
dumi-Component doc generator
qiankun-Micro-Frontends Framework
Ant Motion-Motion Solution
China Mirror 🇨🇳

Community

Awesome Ant Design
Medium
Twitter
yuque logoAnt Design in YuQue
Ant Design in Zhihu
Experience Cloud Blog
seeconf logoSEE Conf-Experience Tech Conference

Help

GitHub
Change Log
FAQ
Bug Report
Issues
Discussions
StackOverflow
SegmentFault

Ant XTech logoMore Products

yuque logoYuQue-Document Collaboration Platform
AntV logoAntV-Data Visualization
Egg logoEgg-Enterprise Node.js Framework
Kitchen logoKitchen-Sketch Toolkit
Galacean logoGalacean-Interactive Graphics Solution
xtech logoAnt Financial Experience Tech
Theme Editor
Made with ❤ by
Ant Group and Ant Design Community
loading

When To Use

  • If you need to represent the switching between two states or on-off state.
  • The difference between Switch and Checkbox is that Switch will trigger a state change directly when you toggle it, while Checkbox is generally used for state marking, which should work in conjunction with submit operation.

Examples

API

Common props ref:Common props

PropertyDescriptionTypeDefaultVersion
autoFocusWhether get focus when component mountedbooleanfalse
checkedDetermine whether the Switch is checkedbooleanfalse
checkedChildrenThe content to be shown when the state is checkedReactNode-
classNameThe additional class to Switchstring-
defaultCheckedWhether to set the initial statebooleanfalse
defaultValueAlias for defaultCheckedboolean-5.12.0
disabledDisable switchbooleanfalse
loadingLoading state of switchbooleanfalse
sizeThe size of the Switch, options: default smallstringdefault
unCheckedChildrenThe content to be shown when the state is uncheckedReactNode-
valueAlias for checkedboolean-5.12.0
onChangeTrigger when the checked state is changingfunction(checked: boolean, event: Event)-
onClickTrigger when clickedfunction(checked: boolean, event: Event)-

Methods

NameDescription
blur()Remove focus
focus()Get focus

Design Token

Component TokenHow to use?
Token NameDescriptionTypeDefault Value
handleBgBackground color of Switch handlestring#fff
handleShadowShadow of Switch handlestring0 2px 4px 0 rgba(0,35,11,0.2)
handleSizeSize of Switch handlenumber18
handleSizeSMSize of small Switch handlenumber12
innerMaxMarginMaximum margin of content areanumber24
innerMaxMarginSMMaximum margin of content area of small Switchnumber18
innerMinMarginMinimum margin of content areanumber9
innerMinMarginSMMinimum margin of content area of small Switchnumber6
trackHeightHeight of Switchstring | number22
trackHeightSMHeight of small Switchstring | number16
trackMinWidthMinimum width of Switchstring | number44
trackMinWidthSMMinimum width of small Switchstring | number28
trackPaddingPadding of Switchnumber2
Global TokenHow to use?

FAQ

Why not work in Form.Item?

Form.Item default bind value to value property, but Switch value property is checked. You can use valuePropName to change bind property.

tsx
<Form.Item name="fieldA" valuePropName="checked">
<Switch />
</Form.Item>
Basic

The most basic usage.

CodeSandbox Icon
codepen icon
External Link Icon
expand codeexpand code
Text & icon

With text and icon.

CodeSandbox Icon
codepen icon
External Link Icon
expand codeexpand code
Loading

Mark a pending state of switch.

CodeSandbox Icon
codepen icon
External Link Icon
expand codeexpand code
Disabled

Disabled state of Switch.

CodeSandbox Icon
codepen icon
External Link Icon
expand codeexpand code
Two sizes

size="small" represents a small sized switch.

CodeSandbox Icon
codepen icon
External Link Icon
expand codeexpand code