Switch
Switching Selector.
When To Use#
If you need to represent the switching between two states or on-off state.
The difference between
Switch
andCheckbox
is thatSwitch
will trigger a state change directly when you toggle it, whileCheckbox
is generally used for state marking, which should work in conjunction with submit operation.
Examples
import { Switch } from 'antd';
function onChange(checked) {
console.log(`switch to ${checked}`);
}
ReactDOM.render(
<Switch defaultChecked={false} onChange={onChange} />,
mountNode
);
关
0
0
import { Switch, Icon } from 'antd';
ReactDOM.render(<div>
<Switch checkedChildren="开" unCheckedChildren="关" />
<br />
<Switch checkedChildren="1" unCheckedChildren="0" />
<br />
<Switch checkedChildren={<Icon type="check" />} unCheckedChildren={<Icon type="cross" />} />
</div>, mountNode);
import { Switch, Button } from 'antd';
class App extends React.Component {
state = {
disabled: true,
}
toggle = () => {
this.setState({
disabled: !this.state.disabled,
});
}
render() {
return (
<div>
<Switch disabled={this.state.disabled} />
<br />
<Button type="primary" onClick={this.toggle}>Toggle disabled</Button>
</div>
);
}
}
ReactDOM.render(<App />, mountNode);
import { Switch } from 'antd';
ReactDOM.render(
<div>
<Switch />
<br />
<Switch size="small" />
</div>
, mountNode);
API#
Switch#
Property | Description | Type | Default |
---|---|---|---|
checked | determine whether the Switch is checked | boolean | false |
defaultChecked | to set the initial state | boolean | false |
onChange | a callback function, can be executed when the checked state is changing | Function(checked:Boolean) | |
checkedChildren | content to be shown when the state is checked | string|ReactNode | |
unCheckedChildren | content to be shown when the state is unchecked | string|ReactNode | |
size | the size of the Switch , options: default small | string | default |