In this lesson, you will learn how to create a custom multi-select checkbox group by combining standard TouchableOpacity and View components.
React Native does not provide a primitive <Checkbox /> component like HTML does. To simulate one, we must manually manage a list of selected items using state and visual indicators. A checkbox group is essentially a collection of items where each item tracks its own "checked" or "unchecked" state.
The Logic of Selection
To build this, we maintain an array of IDs in our state that represents the currently selected items. When a user taps a specific component, we check if its ID already exists in our array. If it does, we remove it (uncheck); if it doesn’t, we add it (select).
Implementation Pattern
You can represent a checkbox using a View with a border. You then swap its background color or add a child element inside based on its selection status:
const [selectedIds, setSelectedIds] = useState([]);
const toggleItem = (id) => {
if (selectedIds.includes(id)) {
setSelectedIds(selectedIds.filter(item => item !== id));
} else {
setSelectedIds([...selectedIds, id]);
}
};
// Rendering an item
<TouchableOpacity onPress={() => toggleItem(item.id)}>
<View style={[
styles.checkbox,
{ backgroundColor: selectedIds.includes(item.id) ? 'blue' : 'white' }
]} />
<Text>{item.label}</Text>
</TouchableOpacity>
Why Manual Construction?
By building this from primitives, you gain total control over the visual feedback. You can use the TouchableOpacity styles we covered in earlier lessons to ensure the hit area is responsive and the visual transition feels native. Since we aren't using a pre-packaged component, you can easily handle edge cases, like disabling specific options or changing the checkbox shape based on your existing design system’s typography and layout constraints.
Because the state is just an array, your UI remains in perfect sync with your data—whenever selectedIds updates, the list re-renders to reflect the new state of each "checkbox."
Key Takeaway: Simulating a checkbox group involves maintaining an array of selected IDs in state and using conditional styling to visually toggle the appearance of a TouchableOpacity component.