---
title: "React Forms Switch Example | Mobiscroll"
component: "Forms"
framework: "React"
library_version: "6.1.5"
url: "https://demo.mobiscroll.com/react/forms/switch"
---

# Switch - React Forms demo

React switch/toggle demo with or without description. Alternative to the checkbox. Supports disabled styling. For React and Next.JS.

- [Live demo](https://demo.mobiscroll.com/react/forms/switch)
- [View React JS code on GitHub](https://github.com/acidb/mobiscroll-demos-react/tree/main/src/demos/form-components/forms/switch)
- [View React TS code on GitHub](https://github.com/acidb/mobiscroll-demos-react-ts/tree/main/src/demos/form-components/forms/switch)
- This demo in other frameworks: [JavaScript](https://demo.mobiscroll.com/javascript/forms/switch.md), [jQuery](https://demo.mobiscroll.com/jquery/forms/switch.md), [Angular](https://demo.mobiscroll.com/angular/forms/switch.md), [Vue](https://demo.mobiscroll.com/vue/forms/switch.md)

## Description

Use the switch for turning options and setting fields on/off. Different color presets are available.

## What this demo shows

- This demo shows examples for the switch component.
- **Switch states** Switch controls displayed in both on and off states.
- **Labels and descriptions** Switches paired with titles and descriptive text.
- **Disabled states** Disabled switches displayed in both on and off states.
- **Color presets** Switches using the primary, secondary, success, danger, warning, and info color presets.

## Best for

- **Settings and preferences** Let users enable or disable configurable options.
- **Feature controls** Turn individual features or modes on and off.
- **Notification preferences** Manage binary choices for notification categories such as email, push, or reminders.
- **Privacy and permissions** Present clear on/off choices for consent, visibility, or access settings.

## Tutorial

```html
<Switch label="Switch Off" description="This is description for Switch" />
<Switch label="Switch On" description="This is description for Switch 2" defaultChecked={true} />
<Switch label="Disabled Switch Off" disabled={true} />
<Switch label="Disabled Switch On" disabled={true} defaultChecked={true} />
```

```html
<Switch label="Primary" color="primary" defaultChecked={true} />
<Switch label="Secondary" color="secondary" defaultChecked={true} />
<Switch label="Success" color="success" defaultChecked={true} />
<Switch label="Danger" color="danger" defaultChecked={true} />
<Switch label="Warning" color="warning" defaultChecked={true} />
<Switch label="Info" color="info" defaultChecked={true} />
```

## Code

### JSX

```jsx
import { Page, setOptions, Switch } from '@mobiscroll/react';

setOptions({
  theme: 'ios',
  themeVariant: 'light'
});

function App() {
  return (
    <Page>
      <div className="mbsc-form-group">
        <div className="mbsc-form-group-title">Switch</div>
        <Switch label="Switch Off" description="This is description for Switch" />
        <Switch label="Switch On" description="This is description for Switch 2" defaultChecked={true} />
        <Switch label="Disabled Switch Off" disabled={true} />
        <Switch label="Disabled Switch On" disabled={true} defaultChecked={true} />
      </div>
      <div className="mbsc-form-group">
        <div className="mbsc-form-group-title">Switch colors</div>
        <Switch label="Primary" color="primary" defaultChecked={true} />
        <Switch label="Secondary" color="secondary" defaultChecked={true} />
        <Switch label="Success" color="success" defaultChecked={true} />
        <Switch label="Danger" color="danger" defaultChecked={true} />
        <Switch label="Warning" color="warning" defaultChecked={true} />
        <Switch label="Info" color="info" defaultChecked={true} />
      </div>
    </Page>
  );
}

export default App;
```

### TSX

```tsx
import { Page, setOptions, Switch } from '@mobiscroll/react';
import { FC } from 'react';

setOptions({
  theme: 'ios',
  themeVariant: 'light'
});

const App: FC = () => (
  <Page>
    <div className="mbsc-form-group">
      <div className="mbsc-form-group-title">Switch</div>
      <Switch label="Switch Off" description="This is description for Switch" />
      <Switch label="Switch On" description="This is description for Switch 2" defaultChecked={true} />
      <Switch label="Disabled Switch Off" disabled={true} />
      <Switch label="Disabled Switch On" disabled={true} defaultChecked={true} />
    </div>
    <div className="mbsc-form-group">
      <div className="mbsc-form-group-title">Switch colors</div>
      <Switch label="Primary" color="primary" defaultChecked={true} />
      <Switch label="Secondary" color="secondary" defaultChecked={true} />
      <Switch label="Success" color="success" defaultChecked={true} />
      <Switch label="Danger" color="danger" defaultChecked={true} />
      <Switch label="Warning" color="warning" defaultChecked={true} />
      <Switch label="Info" color="info" defaultChecked={true} />
    </div>
  </Page>
);
export default App;
```

## Implementation notes

- The on/off state is controlled by `checked`/`defaultChecked` (React `defaultChecked={true}`; JS/jQuery `checked` attribute on `<input type="checkbox" mbsc-switch>`; Angular/Vue bind it through two-way binding - see below).
- `label` sets the switch's title text (JS/jQuery: `data-label` attribute).
- `description` sets secondary helper text shown under the label (JS/jQuery: `data-description` attribute).
- `disabled` renders the switch non-interactive in either the on or off state; combine with `checked`/`defaultChecked` to show a disabled-on switch.
- `color` applies a preset accent color to the track/thumb when on: `'primary'`, `'secondary'`, `'success'`, `'danger'`, `'warning'`, or `'info'`.
- Framework state binding differs: JS/jQuery both read/write state imperatively via the shared `mobiscroll.getInst(element).checked` helper; jQuery additionally offers the chained form `$(element).mobiscroll('setVal', ...)` (not available in plain JS); React uses `defaultChecked`/`checked` + `onChange`; Angular binds via `[(ngModel)]="value"` for two-way sync; Vue binds via `v-model="value"`.
- Framework markup differences: JS/jQuery wraps a plain `<input type="checkbox" mbsc-switch>` in a `<label>` with `data-*` attributes for `label`/`description`/`color`; React uses `<Switch label="..." description="..." color="primary" disabled={true} defaultChecked={true} />`; Angular uses `<mbsc-switch label="..." color="primary" [(ngModel)]="value" [disabled]="true">`; Vue uses `<MbscSwitch v-model="value" label="..." color="primary" />` (with `disabled` set separately per switch since it isn't part of the `v-model` binding).
- Grouping switches under a shared `mbsc-form-group` with an `mbsc-form-group-title` is a layout convention for the demo, not a switch option - each switch instance is independently configured.

## Run it in your project

1. If not signed up yet, or don't own a license, you can [start a free trial](https://mobiscroll.com/starttrial).

2. Clone the repository:

   ```
   git clone https://github.com/acidb/mobiscroll-demos-react.git
   ```

3. If not installed yet, install the Mobiscroll CLI from NPM:

   ```
   npm install -g @mobiscroll/cli
   ```

4. Config Mobiscroll - open a terminal window in the root directory and run:

   ```
   mobiscroll config react
   ```

5. Install dependencies - open a terminal window in the root directory and run:

   ```
   npm install
   ```

6. Run the project:

   ```
   npm run dev
   ```

On a trial, add `--trial` to the config command: `mobiscroll config react --trial`.

Running a demo locally requires a Mobiscroll trial or a license; setup is per framework at https://mobiscroll.com/docs/react/getting-started/installation

## Related demos

- Previous: [Checkbox](https://demo.mobiscroll.com/react/forms/checkbox.md) - React checkbox and checklist demo and styling with or without description text. Supports disabled styling.
- Next: [Radio buttons](https://demo.mobiscroll.com/react/forms/radio-button.md) - React radio button demo for single select option lists. Use it with or without description, supports disabled styling.
