Bird
Raised Fist0
React Nativemobile~20 mins

Passing parameters between screens in React Native - Mini App: Build & Ship

Choose your learning style10 modes available
Build: User Detail Screen
Build two screens: a User List screen and a User Detail screen. When a user taps a name on the list, navigate to the detail screen and show the selected user's name and age passed as parameters.
Target UI
User List Screen:
-----------------
| User List      |
| 1. Alice       |
| 2. Bob         |
| 3. Carol       |
-----------------

User Detail Screen:
-----------------
| User Detail    |
| Name: Alice    |
| Age: 25        |
| [Back Button]  |
-----------------
Create a User List screen with a list of user names.
When a user taps a name, navigate to User Detail screen.
Pass the user's name and age as parameters during navigation.
Display the passed name and age on the User Detail screen.
Add a back button to return to the User List screen.
Starter Code
React Native
import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { View, Text, FlatList, TouchableOpacity, Button } from 'react-native';

const Stack = createNativeStackNavigator();

function UserListScreen({ navigation }) {
  const users = [
    { id: '1', name: 'Alice', age: 25 },
    { id: '2', name: 'Bob', age: 30 },
    { id: '3', name: 'Carol', age: 22 },
  ];

  // TODO: Render list of users with TouchableOpacity
  // TODO: On press, navigate to UserDetailScreen passing name and age

  return (
    <View style={{ flex: 1, padding: 20 }}>
      <Text style={{ fontSize: 24, marginBottom: 20 }}>User List</Text>
      {/* TODO: Add FlatList here */}
    </View>
  );
}

function UserDetailScreen({ route, navigation }) {
  // TODO: Extract name and age from route.params

  return (
    <View style={{ flex: 1, padding: 20 }}>
      <Text style={{ fontSize: 24, marginBottom: 20 }}>User Detail</Text>
      {/* TODO: Show name and age here */}
      {/* TODO: Add a back button */}
    </View>
  );
}

export default function App() {
  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen name="UserList" component={UserListScreen} />
        <Stack.Screen name="UserDetail" component={UserDetailScreen} />
      </Stack.Navigator>
    </NavigationContainer>
  );
}
Task 1
Task 2
Task 3
Task 4
Task 5
Solution
React Native
import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { View, Text, FlatList, TouchableOpacity, Button } from 'react-native';

const Stack = createNativeStackNavigator();

function UserListScreen({ navigation }) {
  const users = [
    { id: '1', name: 'Alice', age: 25 },
    { id: '2', name: 'Bob', age: 30 },
    { id: '3', name: 'Carol', age: 22 },
  ];

  return (
    <View style={{ flex: 1, padding: 20 }}>
      <Text style={{ fontSize: 24, marginBottom: 20 }}>User List</Text>
      <FlatList
        data={users}
        keyExtractor={item => item.id}
        renderItem={({ item }) => (
          <TouchableOpacity
            onPress={() => navigation.navigate('UserDetail', { name: item.name, age: item.age })}
            style={{ paddingVertical: 10 }}
            accessibilityRole="button"
            accessibilityLabel={`View details for ${item.name}`}
          >
            <Text style={{ fontSize: 18 }}>{item.name}</Text>
          </TouchableOpacity>
        )}
      />
    </View>
  );
}

function UserDetailScreen({ route, navigation }) {
  const { name, age } = route.params;

  return (
    <View style={{ flex: 1, padding: 20 }}>
      <Text style={{ fontSize: 24, marginBottom: 20 }}>User Detail</Text>
      <Text style={{ fontSize: 18, marginBottom: 10 }}>Name: {name}</Text>
      <Text style={{ fontSize: 18, marginBottom: 20 }}>Age: {age}</Text>
      <Button title="Back" onPress={() => navigation.goBack()} accessibilityLabel="Go back to user list" />
    </View>
  );
}

export default function App() {
  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen name="UserList" component={UserListScreen} />
        <Stack.Screen name="UserDetail" component={UserDetailScreen} />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

We created two screens: UserListScreen and UserDetailScreen. The UserListScreen shows a list of users using FlatList. Each user name is wrapped in a TouchableOpacity so it can be tapped. When tapped, it navigates to the UserDetailScreen and passes the user's name and age as parameters.

In UserDetailScreen, we get the parameters from route.params and display them in Text components. We also added a Back button that calls navigation.goBack() to return to the list.

This shows how to pass data between screens in React Native using React Navigation.

Final Result
Completed Screen
User List Screen:
-----------------
| User List      |
| 1. Alice       |
| 2. Bob         |
| 3. Carol       |
-----------------

User Detail Screen:
-----------------
| User Detail    |
| Name: Alice    |
| Age: 25        |
| [Back Button]  |
-----------------
Tap a user name on User List screen to open User Detail screen with that user's info.
Tap Back button on User Detail screen to return to User List screen.
Stretch Goal
Add a header title on User Detail screen that shows the user's name dynamically.
💡 Hint
Use navigation.setOptions inside UserDetailScreen to set the header title using the passed name.

Practice

(1/5)
1. What is the correct way to pass a parameter named userId with value 42 when navigating to a screen called Profile in React Native?
easy
A. navigation.move('Profile', { userId })
B. navigation.goTo('Profile', userId=42)
C. navigation.push('Profile', userId=42)
D. navigation.navigate('Profile', { userId: 42 })

Solution

  1. Step 1: Understand navigation method

    To pass parameters, use navigation.navigate with the screen name and an object of params.
  2. Step 2: Check parameter syntax

    The parameter must be inside an object like { userId: 42 } as the second argument.
  3. Final Answer:

    navigation.navigate('Profile', { userId: 42 }) -> Option D
  4. Quick Check:

    Pass params with navigate(screen, { params }) [OK]
Hint: Use navigate with screen name and params object [OK]
Common Mistakes:
  • Using wrong method like goTo or move
  • Passing params outside an object
  • Forgetting to include params argument
2. Which of the following is the correct syntax to access a parameter named itemId inside a React Native screen component?
easy
A. const itemId = props.navigation.params.itemId;
B. const itemId = navigation.route.params.itemId;
C. const itemId = route.params.itemId;
D. const itemId = params.route.itemId;

Solution

  1. Step 1: Identify how params are accessed

    In React Navigation, the screen receives a route prop containing params.
  2. Step 2: Use correct property chain

    Access the parameter with route.params.itemId.
  3. Final Answer:

    const itemId = route.params.itemId; -> Option C
  4. Quick Check:

    Use route.params to get passed parameters [OK]
Hint: Use route.params to access passed data [OK]
Common Mistakes:
  • Trying to get params from navigation instead of route
  • Incorrect property order like navigation.route.params
  • Using props.navigation.params which is invalid
3. Given this code snippet in a React Native app:
navigation.navigate('Details', { productId: 101 });

// In Details screen component:
const Details = ({ route }) => {
  return {route.params.productId};
};
What will be displayed on the screen?
medium
A. productId
B. 101
C. route.params.productId
D. undefined

Solution

  1. Step 1: Understand parameter passing

    The productId with value 101 is passed to the Details screen via navigation.navigate.
  2. Step 2: Check how parameter is used in Details

    The component accesses route.params.productId and displays it inside a Text component.
  3. Final Answer:

    101 -> Option B
  4. Quick Check:

    Passed param value is displayed [OK]
Hint: route.params contains passed values to display [OK]
Common Mistakes:
  • Expecting param name string instead of value
  • Using undefined or wrong property to access param
  • Not passing params correctly in navigate
4. What is wrong with this code snippet that tries to pass a parameter name to a screen called Welcome?
navigation.navigate('Welcome', name: 'Alice');
medium
A. Parameters must be passed inside an object as the second argument.
B. The screen name should be lowercase.
C. The parameter name cannot be a string.
D. navigate method does not accept parameters.

Solution

  1. Step 1: Check navigate method syntax

    The second argument to navigate must be an object containing parameters.
  2. Step 2: Identify error in parameter passing

    The code passes name: 'Alice' without wrapping it in curly braces, which is invalid syntax.
  3. Final Answer:

    Parameters must be passed inside an object as the second argument. -> Option A
  4. Quick Check:

    Params go inside an object in navigate [OK]
Hint: Always wrap params in an object when navigating [OK]
Common Mistakes:
  • Passing params without curly braces
  • Using wrong syntax for navigate arguments
  • Assuming screen names must be lowercase
5. You want to navigate from a Home screen to a Profile screen and pass both userId and userName. How should you write the navigation call to pass these parameters correctly?
hard
A. navigation.navigate('Profile', { userId: 5, userName: 'John' })
B. navigation.navigate('Profile', userId=5, userName='John')
C. navigation.navigate('Profile', ['userId', 5, 'userName', 'John'])
D. navigation.navigate('Profile', { 'userId': 5 }, { 'userName': 'John' })

Solution

  1. Step 1: Understand parameter object structure

    Multiple parameters must be passed inside a single object with key-value pairs.
  2. Step 2: Check each option's syntax

    navigation.navigate('Profile', { userId: 5, userName: 'John' }) correctly passes both userId and userName inside one object.
  3. Final Answer:

    navigation.navigate('Profile', { userId: 5, userName: 'John' }) -> Option A
  4. Quick Check:

    Pass multiple params inside one object [OK]
Hint: Put all params inside one object in navigate call [OK]
Common Mistakes:
  • Passing params as separate arguments
  • Using array instead of object for params
  • Splitting params into multiple objects
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