Switches and checkboxes let users turn options on or off easily. They help make apps interactive and clear.
Switch and checkbox patterns in React Native
import React, { useState } from 'react'; import { View, Switch, Text, Pressable } from 'react-native'; function MyComponent() { const [isSwitchOn, setIsSwitchOn] = useState(false); const [isChecked, setIsChecked] = useState(false); return ( <View> <Switch value={isSwitchOn} onValueChange={setIsSwitchOn} /> <Pressable onPress={() => setIsChecked(!isChecked)} style={{ width: 44, height: 44, justifyContent: 'center', alignItems: 'center' }} > <View style={{ width: 24, height: 24, borderWidth: 2, borderColor: isChecked ? '#007AFF' : '#ccc', backgroundColor: isChecked ? '#007AFF' : 'transparent', borderRadius: 4 }} /> </Pressable> </View> ); }
Switch is used for toggling between two states, like ON/OFF.
Custom CheckBox (using Pressable + View) is used for selecting or deselecting an option.
const [isEnabled, setIsEnabled] = useState(false);
<Switch
value={isEnabled}
onValueChange={setIsEnabled}
/>const [checked, setChecked] = useState(false);
<Pressable
onPress={() => setChecked(!checked)}
style={{width: 44, height: 44, justifyContent: 'center', alignItems: 'center'}}
>
<View
style={{
width: 24,
height: 24,
borderWidth: 2,
borderColor: checked ? '#007AFF' : '#ccc',
backgroundColor: checked ? '#007AFF' : 'transparent',
borderRadius: 4
}}
/>
</Pressable><Switch
value={true}
disabled={true}
/>This app shows a switch to turn notifications on or off and a custom checkbox to accept terms. The text below updates to show the current choices. The checkbox is made accessible with roles and states.
import React, { useState } from 'react'; import { View, Text, Switch, Pressable, StyleSheet } from 'react-native'; export default function App() { const [isSwitchOn, setIsSwitchOn] = useState(false); const [isChecked, setIsChecked] = useState(false); return ( <View style={styles.container}> <Text style={styles.title}>Switch and Checkbox Example</Text> <View style={styles.row}> <Text style={styles.label}>Enable Notifications</Text> <Switch value={isSwitchOn} onValueChange={setIsSwitchOn} accessibilityLabel="Enable Notifications Switch" /> </View> <Pressable style={styles.checkboxContainer} onPress={() => setIsChecked(!isChecked)} accessibilityRole="checkbox" accessibilityState={{ checked: isChecked }} accessibilityLabel="Accept Terms Checkbox" > <View style={[styles.checkbox, isChecked && styles.checked]} /> <Text style={styles.label}>Accept Terms and Conditions</Text> </Pressable> <Text style={styles.status}> Notifications are {isSwitchOn ? 'ON' : 'OFF'} </Text> <Text style={styles.status}> Terms accepted: {isChecked ? 'Yes' : 'No'} </Text> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', padding: 20, backgroundColor: '#fff' }, title: { fontSize: 24, marginBottom: 20, fontWeight: 'bold', textAlign: 'center' }, row: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', marginBottom: 20 }, label: { fontSize: 18 }, checkboxContainer: { flexDirection: 'row', alignItems: 'center', marginBottom: 20 }, checkbox: { width: 24, height: 24, borderWidth: 2, borderColor: '#555', marginRight: 10, borderRadius: 4 }, checked: { backgroundColor: '#007AFF', borderColor: '#007AFF' }, status: { fontSize: 16, marginTop: 10 } });
React Native's built-in CheckBox is deprecated on some platforms; a custom checkbox using Pressable and View can be used instead.
Always add accessibility labels and roles so screen readers can describe switches and checkboxes.
Use state hooks like useState to track the on/off or checked/unchecked status.
Switches toggle between two states, like ON/OFF.
Checkboxes let users select or deselect options.
Use state to control their values and update UI accordingly.
Practice
Switch component in React Native?Solution
Step 1: Understand the Switch component
A Switch is a UI element that lets users toggle between two states, usually ON or OFF.Step 2: Compare with other components
Checkboxes allow multiple selections, text inputs accept text, and image galleries display images, so these do not match the Switch's purpose.Final Answer:
To toggle between two states like ON and OFF -> Option AQuick Check:
Switch toggles states = B [OK]
- Confusing Switch with Checkbox
- Thinking Switch accepts text input
- Assuming Switch displays images
Switch component in React Native?Solution
Step 1: Recall Switch props in React Native
The Switch component usesvalueto set its state andonValueChangeas the event handler for toggling.Step 2: Identify correct syntax
Options usingcheckedoronChangeare incorrect because React Native Switch does not use these props.Final Answer:
<Switch value={isOn} onValueChange={() => setIsOn(!isOn)} /> -> Option DQuick Check:
Switch uses value and onValueChange = C [OK]
- Using checked instead of value
- Using onChange instead of onValueChange
- Mixing HTML input props with React Native Switch
const [checked, setChecked] = React.useState(false);
return (
<CheckBox
value={checked}
onValueChange={() => setChecked(!checked)}
/>
);
If the user taps the checkbox once, what is the new value of checked?Solution
Step 1: Initial state value
The state variablecheckedstarts asfalse.Step 2: Effect of tapping checkbox
TheonValueChangetogglescheckedto its opposite, so it becomestrueafter one tap.Final Answer:
true -> Option BQuick Check:
Toggle false once = true [OK]
- Assuming state stays false after tap
- Confusing value prop with event
- Thinking onValueChange returns new value
const [isEnabled, setIsEnabled] = React.useState(false);
return (
<Switch
value={isEnabled}
onChange={() => setIsEnabled(!isEnabled)}
/>
);Solution
Step 1: Check event handler prop
The Switch component usesonValueChangeto handle toggle events, notonChange.Step 2: Verify other parts
The initial state is declared,valueis correct, and React import is assumed correct.Final Answer:
Using onChange instead of onValueChange -> Option CQuick Check:
Switch needs onValueChange, not onChange [OK]
- Using onChange event instead of onValueChange
- Confusing value and checked props
- Forgetting to declare state
Solution
Step 1: Understand single selection requirement
Only one checkbox can be selected at a time, so all must share one state that tracks which is selected.Step 2: Evaluate options
Use three CheckBox components sharing one state variable that stores the selected option. This uses one state variable to store the selected option and updates it on selection, ensuring only one is selected. Options A and C use independent states allowing multiple selections. Use one CheckBox component and change its label dynamically uses only one checkbox, which cannot represent three options.Final Answer:
Use three CheckBox components sharing one state variable that stores the selected option -> Option AQuick Check:
Single selection needs shared state = D [OK]
- Giving each checkbox its own state allowing multiple selections
- Using Switch instead of CheckBox for selection list
- Trying to reuse one checkbox for multiple options
Ready to prove it?
Pass the test and this pattern moves unknown
