Toggles, Switches, and Checkboxes
Use the Switch component to capture boolean preferences and build a custom checkbox by toggling state and updating a styled View.
The Switch Component
React Native's built-in Switch component renders the platform's native toggle control — a pill-shaped slider on iOS and a Material Design toggle on Android. It is a controlled component: you must manage its state with useState and provide both the value prop (the current boolean) and the onValueChange callback. Switch is ideal for simple on/off preferences like enabling notifications, dark mode, location access, or auto-save — anywhere a binary choice is needed.
import { Switch, View, Text, StyleSheet } from 'react-native';
import { useState } from 'react';
export default function NotificationsToggle() {
const [enabled, setEnabled] = useState(true);
return (
<View style={styles.row}>
<Text style={styles.label}>Push Notifications</Text>
<Switch
value={enabled}
onValueChange={setEnabled} // same as (val) => setEnabled(val)
/>
</View>
);
}
const styles = StyleSheet.create({
row: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', padding: 16, backgroundColor: '#fff', borderRadius: 12 },
label: { fontSize: 16, color: '#333' },
});Styling the Switch
The Switch component accepts color props to customize its appearance. trackColor takes an object with false and true keys for the track color in each state. thumbColor sets the knob color (use a function of the current value for dynamic coloring on Android). ios_backgroundColor sets the track color when the switch is off on iOS (since iOS ignores trackColor.false in some versions). These props let you match the switch to your app's brand colors rather than using the default system blue.
import { Switch, View, Text, StyleSheet } from 'react-native';
import { useState } from 'react';
export default function BrandedSwitch() {
const [active, setActive] = useState(false);
return (
<View style={styles.row}>
<Text style={styles.label}>Dark Mode</Text>
<Switch
value={active}
onValueChange={setActive}
trackColor={{ false: '#e0e0e0', true: '#4f86f7' }}
thumbColor={active ? '#fff' : '#f0f0f0'}
ios_backgroundColor='#e0e0e0'
/>
</View>
);
}
const styles = StyleSheet.create({
row: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', padding: 16 },
label: { fontSize: 16 },
});All lessons in this course
- TextInput Basics and Keyboard Types
- Tap Handlers with TouchableOpacity and Pressable
- Toggles, Switches, and Checkboxes
- Building a Simple Login Form