Bird
Raised Fist0
React Nativemobile~20 mins

Custom tab bars in React Native - Mini App: Build & Ship

Choose your learning style10 modes available
Build: CustomTabBarScreen
A screen with a custom tab bar at the bottom that lets users switch between Home, Search, and Profile tabs. The selected tab is highlighted.
Target UI
┌─────────────────────────────┐
│                             │
│        [Content Area]        │
│                             │
│                             │
│                             │
│                             │
│                             │
│                             │
├─────────────────────────────┤
│ Home | Search | Profile     │
│  (icon) (icon)  (icon)      │
│  [selected tab highlighted] │
└─────────────────────────────┘
Create a bottom tab bar with three tabs: Home, Search, Profile
Each tab shows an icon and label
Highlight the selected tab with a different color
Switch content area text to match selected tab
Use TouchableOpacity for tab buttons
Manage selected tab state with React useState hook
Starter Code
React Native
import React, { useState } from 'react';
import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';

export default function CustomTabBarScreen() {
  const [selectedTab, setSelectedTab] = useState('Home');

  return (
    <View style={styles.container}>
      {/* TODO: Add content area showing selected tab name */}

      {/* TODO: Add custom tab bar with 3 tabs */}
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'space-between',
    paddingBottom: 20
  }
});
Task 1
Task 2
Task 3
Task 4
Task 5
Solution
React Native
import React, { useState } from 'react';
import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';

export default function CustomTabBarScreen() {
  const [selectedTab, setSelectedTab] = useState('Home');

  const tabs = [
    { key: 'Home', label: 'Home', icon: '🏠' },
    { key: 'Search', label: 'Search', icon: '🔍' },
    { key: 'Profile', label: 'Profile', icon: '👤' }
  ];

  return (
    <View style={styles.container}>
      <View style={styles.content}>
        <Text style={styles.contentText}>You are on the {selectedTab} tab</Text>
      </View>

      <View style={styles.tabBar}>
        {tabs.map(tab => {
          const isSelected = selectedTab === tab.key;
          return (
            <TouchableOpacity
              key={tab.key}
              style={styles.tabButton}
              onPress={() => setSelectedTab(tab.key)}
              accessibilityRole="button"
              accessibilityState={{ selected: isSelected }}
              accessibilityLabel={`${tab.label} tab`}
            >
              <Text style={[styles.icon, isSelected && styles.selected]}>{tab.icon}</Text>
              <Text style={[styles.label, isSelected && styles.selected]}>{tab.label}</Text>
            </TouchableOpacity>
          );
        })}
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'space-between',
    paddingBottom: 20,
    backgroundColor: '#fff'
  },
  content: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center'
  },
  contentText: {
    fontSize: 24,
    color: '#333'
  },
  tabBar: {
    flexDirection: 'row',
    justifyContent: 'space-around',
    borderTopWidth: 1,
    borderTopColor: '#ccc',
    paddingVertical: 10,
    backgroundColor: '#f9f9f9'
  },
  tabButton: {
    alignItems: 'center'
  },
  icon: {
    fontSize: 24,
    color: '#666'
  },
  label: {
    fontSize: 12,
    color: '#666',
    marginTop: 4
  },
  selected: {
    color: '#007AFF',
    fontWeight: '600'
  }
});

This solution uses React Native's useState hook to keep track of the selected tab. We define an array of tabs with keys, labels, and emoji icons for simplicity.

The content area shows a text that updates based on the selected tab.

The custom tab bar is a View with three TouchableOpacity buttons. Each button shows an icon and label. When a tab is selected, its icon and label change color to blue (#007AFF) and become bold to highlight it.

Accessibility roles and states are added for better screen reader support.

This approach is simple and clear for beginners to understand how to build a custom tab bar without external libraries.

Final Result
Completed Screen
┌─────────────────────────────┐
│                             │
│   You are on the Home tab   │
│                             │
│                             │
│                             │
│                             │
│                             │
│                             │
├─────────────────────────────┤
│ 🏠 Home | 🔍 Search | 👤 Profile │
│  [blue & bold]   normal    normal │
└─────────────────────────────┘
Tapping a tab changes the content text to 'You are on the [TabName] tab'.
The tapped tab's icon and label turn blue and bold to show selection.
Other tabs revert to normal gray color.
Stretch Goal
Add a smooth fade animation when switching tabs content
💡 Hint
Use React Native's Animated API or the LayoutAnimation module to animate the content text opacity on tab change

Practice

(1/5)
1. What is the main purpose of a custom tab bar in React Native navigation?
easy
A. To replace the entire navigation stack with a new one
B. To disable navigation between screens
C. To create a personalized look and behavior for the bottom navigation tabs
D. To automatically fetch data for each tab

Solution

  1. Step 1: Understand what a tab bar does

    A tab bar shows navigation options at the bottom of the screen for switching between views.
  2. Step 2: Identify the role of a custom tab bar

    A custom tab bar lets you change how these tabs look and behave, instead of using the default style.
  3. Final Answer:

    To create a personalized look and behavior for the bottom navigation tabs -> Option C
  4. Quick Check:

    Custom tab bar = personalized navigation UI [OK]
Hint: Custom tab bars change appearance and interaction of bottom tabs [OK]
Common Mistakes:
  • Thinking it replaces navigation logic entirely
  • Assuming it disables navigation
  • Confusing it with data fetching
2. Which React Native component is commonly used to make a tab clickable in a custom tab bar?
easy
A. View
B. TouchableOpacity
C. TextInput
D. ScrollView

Solution

  1. Step 1: Identify clickable components

    TouchableOpacity is designed to respond to touch events and provide feedback.
  2. Step 2: Match with tab interaction needs

    Tabs need to be clickable to switch screens, so TouchableOpacity is the right choice.
  3. Final Answer:

    TouchableOpacity -> Option B
  4. Quick Check:

    Clickable tab = TouchableOpacity [OK]
Hint: Use TouchableOpacity for clickable tabs in React Native [OK]
Common Mistakes:
  • Using View which is not clickable by default
  • Choosing TextInput which is for text input
  • Using ScrollView which is for scrolling content
3. Given this snippet inside a custom tab bar component:
state.index === 1 ? 'blue' : 'gray'
What does this expression do?
medium
A. Causes a syntax error because of missing parentheses
B. Always sets the tab color to gray
C. Changes the tab color to blue when the app starts
D. Sets the tab color to blue if it is the active tab, otherwise gray

Solution

  1. Step 1: Understand the ternary expression

    The expression checks if state.index equals 1; if true, it returns 'blue', else 'gray'.
  2. Step 2: Relate to tab color logic

    This means the tab at index 1 is highlighted blue when active, others are gray.
  3. Final Answer:

    Sets the tab color to blue if it is the active tab, otherwise gray -> Option D
  4. Quick Check:

    state.index === 1 ? 'blue' : 'gray' = active tab color logic [OK]
Hint: Ternary ? : chooses color based on active tab index [OK]
Common Mistakes:
  • Thinking it always returns gray
  • Assuming it causes syntax error
  • Confusing the condition with app start
4. Identify the error in this custom tab bar code snippet:
const CustomTabBar = ({ state, navigation }) => {
  return (
    <View>
      {state.routes.map((route, index) => (
        <TouchableOpacity
          key={index}
          onPress={() => navigation.navigate(route.name)}
        >
          <Text>{route.name}</Text>
        </TouchableOpacity>
      ))}
    </View>
  );
};
medium
A. Missing accessibility props for TouchableOpacity
B. Using index as key causes navigation errors
C. Incorrect use of navigation.navigate inside onPress
D. No error, code works correctly

Solution

  1. Step 1: Review accessibility best practices

    TouchableOpacity should have accessibilityRole and accessibilityState for screen readers.
  2. Step 2: Check other parts of code

    Using index as key is not ideal but won't cause navigation errors; navigation.navigate usage is correct.
  3. Final Answer:

    Missing accessibility props for TouchableOpacity -> Option A
  4. Quick Check:

    Accessibility props missing = error [OK]
Hint: Always add accessibilityRole and accessibilityState to touchables [OK]
Common Mistakes:
  • Ignoring accessibility props
  • Thinking index key breaks navigation
  • Assuming navigation.navigate is wrong here
5. You want to create a custom tab bar that highlights the active tab with a bold label and changes the icon color. Which approach correctly uses state and navigation to achieve this?
hard
A. Use state.index to check active tab, and navigation.navigate(route.name) on press; style label bold if active
B. Use navigation.state.index to check active tab, and state.navigate(route.name) on press; style label bold if active
C. Use state.activeTab to check active tab, and navigation.goBack() on press; style label bold if active
D. Use state.index to check active tab, and navigation.push(route.key) on press; style label bold if active

Solution

  1. Step 1: Identify correct state usage

    The active tab index is in state.index, not navigation.state or state.activeTab.
  2. Step 2: Identify correct navigation method

    navigation.navigate(route.name) is used to switch tabs; goBack or push are not appropriate here.
  3. Step 3: Apply styling based on active tab

    Compare current index with tab index to apply bold style and change icon color.
  4. Final Answer:

    Use state.index to check active tab, and navigation.navigate(route.name) on press; style label bold if active -> Option A
  5. Quick Check:

    state.index + navigation.navigate = correct tab control [OK]
Hint: Use state.index for active tab and navigation.navigate for switching [OK]
Common Mistakes:
  • Using wrong property like navigation.state.index
  • Using navigation.goBack instead of navigate
  • Using state.activeTab which doesn't exist
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard