import { Stack, Text, useSwitch } from "@vortexlabs/vortex-native";
import { Switch } from "react-native";
const Example = () => {
const { isOn, flipSwitch } = useSwitch();
return (
<Stack direction="row" gap={2} align="center">
<Switch aria-label="Notifications" value={isOn} onValueChange={flipSwitch} />
<Text>Notifications {isOn ? "on" : "off"}</Text>
</Stack>
);
};Features
- Manages boolean state with an optional initial value.
- Provides helpers to turn the state on, off, or toggle it.
- Works with
onPressandonValueChangehandlers.
Initial Value
Pass true to start the switch in the on state. Without a value, the switch starts as false.
const enabled = useSwitch(true);
const disabled = useSwitch();
enabled.isOn; // true
disabled.isOn; // falseToggle
Call flipSwitch() to toggle, or pass a boolean to set the state.
const { flipSwitch } = useSwitch();
flipSwitch(); // toggles current state
flipSwitch(true); // turns on
flipSwitch(false); // turns offPass flipSwitch directly to a handler. onPress={flipSwitch} receives a press event and toggles; onValueChange={flipSwitch} receives the next boolean and sets it.