Skip to main content

Common Examples

import { Switch } from "@servicetitan/anvil2";

function ExampleComponent() {
  return <Switch defaultChecked={true} label="Switch" value="switch-example" />;
}

Controlling switches

Uncontrolled switch

By default, the state of a switch is handled internally. Use the defaultChecked prop to set the initial state.To access the current value, a React ref can be used. Make sure you understand the differences between refs and state in React.

Controlled switch

Alternatively, use the checked and onChange props to manually control the checked state.

React Accessibility

  • The supplied label prop should either be text or a component containing text. If this is not possible, then either an aria-label should be set or a visible label should be referenced with aria-labelledby.
  • If the page contains additional text needed to fully understand the purpose of the Switch, this text should be referenced with aria-describedby.
For more guidance on form field labels and context, see input field context association best practices.
Last modified on January 23, 2026