Switch
Switching Selector.
When To Use#
- If you need to represent the switching between two states or on-off state. 
- The difference between - Switchand- Checkboxis that- Switchwill trigger a state change directly when you toggle it, while- Checkboxis generally used for state marking, which should work in conjunction with submit operation.
Examples
TypeScript
JavaScript
import { Switch } from 'antd';
import React from 'react';
const onChange = (checked: boolean) => {
  console.log(`switch to ${checked}`);
};
const App: React.FC = () => <Switch defaultChecked onChange={onChange} />;
export default App;TypeScript
JavaScript
import { CheckOutlined, CloseOutlined } from '@ant-design/icons';
import { Switch } from 'antd';
import React from 'react';
const App: React.FC = () => (
  <>
    <Switch checkedChildren="开启" unCheckedChildren="关闭" defaultChecked />
    <br />
    <Switch checkedChildren="1" unCheckedChildren="0" />
    <br />
    <Switch
      checkedChildren={<CheckOutlined />}
      unCheckedChildren={<CloseOutlined />}
      defaultChecked
    />
  </>
);
export default App;TypeScript
JavaScript
import { Switch } from 'antd';
import React from 'react';
const App: React.FC = () => (
  <>
    <Switch loading defaultChecked />
    <br />
    <Switch size="small" loading />
  </>
);
export default App;TypeScript
JavaScript
import { Button, Switch } from 'antd';
import React, { useState } from 'react';
const App: React.FC = () => {
  const [disabled, setDisabled] = useState(true);
  const toggle = () => {
    setDisabled(!disabled);
  };
  return (
    <>
      <Switch disabled={disabled} defaultChecked />
      <br />
      <Button type="primary" onClick={toggle}>
        Toggle disabled
      </Button>
    </>
  );
};
export default App;TypeScript
JavaScript
import { Switch } from 'antd';
import React from 'react';
const App: React.FC = () => (
  <>
    <Switch defaultChecked />
    <br />
    <Switch size="small" defaultChecked />
  </>
);
export default App;API#
| Property | Description | Type | Default | 
|---|---|---|---|
| autoFocus | Whether get focus when component mounted | boolean | false | 
| checked | Determine whether the Switch is checked | boolean | false | 
| checkedChildren | The content to be shown when the state is checked | ReactNode | - | 
| className | The additional class to Switch | string | - | 
| defaultChecked | Whether to set the initial state | boolean | false | 
| disabled | Disable switch | boolean | false | 
| loading | Loading state of switch | boolean | false | 
| size | The size of the Switch, options: defaultsmall | string | default | 
| unCheckedChildren | The content to be shown when the state is unchecked | ReactNode | - | 
| onChange | Trigger when the checked state is changing | function(checked: boolean, event: Event) | - | 
| onClick | Trigger when clicked | function(checked: boolean, event: Event) | - | 
Methods#
| Name | Description | 
|---|---|
| blur() | Remove focus | 
| focus() | Get focus |