Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 71 ยท Navigation

Route Params and Typed Navigation

Pass data between screens with route.params, type every route with a param list, update params with setParams, return results and use the navigation hooks.

A list screen is only useful if the detail screen knows which item you tapped. In React Navigation, data travels between screens as route params: a small object you hand over when you navigate, and that the target screen reads from route.params.

Params are simple, but they are also where navigation code goes wrong: typos in names, missing fields, objects that are too big to put in a URL. This lesson shows the basic flow, then adds TypeScript so the compiler checks every navigate call, then covers updating params, sending a result back, and the hooks you use deep inside a component tree. It builds on Stack Navigation with React Navigation.

What runs on this site. The real navigation code is read-only because it needs a native runtime. The runnable examples are stand-ins: small reducers that follow the same param rules (params are stored on the route, setParams merges, a result can be merged into an earlier route). Types are shown in read-only TypeScript blocks, since the browser stand-ins are plain JavaScript.

Sending and reading params

The second argument of navigate and push is the params object:

JSX
function ProductList({ navigation }) {
  return (
    <FlatList
      data={products}
      keyExtractor={(p) => String(p.id)}
      renderItem={({ item }) => (
        <Pressable onPress={() => navigation.navigate('Product', { id: item.id, name: item.name })}>
          <Text>{item.name}</Text>
        </Pressable>
      )}
    />
  );
}

function ProductScreen({ route }) {
  const { id, name } = route.params;
  return <Text>Product #{id}: {name}</Text>;
}

Every screen component gets two props: navigation (the remote control) and route (the description of this screen instance: route.name, route.key and route.params).

You can also give a screen default params with initialParams, which are used when none are passed:

JSX
<Stack.Screen name="Product" component={ProductScreen} initialParams={{ id: 1 }} />

What belongs in params

Keep params small and serializable: ids, names, filters, page numbers. React Navigation can save and restore the whole navigation state, and the same params become part of a URL when you add deep linking. That means no functions, class instances, or large data objects. Pass { id: 42 } and load the product in the target screen from your cache or API. Params that contain a whole product would go stale the moment the product changes elsewhere.

Params on a stack (stand-in)

Typing navigation with TypeScript

Without types, navigate('Product', { idd: 3 }) compiles and then crashes. The fix is a param list: one type that maps every route name to its params type.

TypeScript
import type { NativeStackScreenProps } from '@react-navigation/native-stack';

export type RootStackParamList = {
  Home: undefined;                          // no params
  Product: { id: number; name?: string };   // id required, name optional
  Search: { query: string } | undefined;    // params are optional here
};

export type ProductProps = NativeStackScreenProps<RootStackParamList, 'Product'>;

Pass the list to the navigator and type each screen with the matching props:

TSX
const Stack = createNativeStackNavigator<RootStackParamList>();

function ProductScreen({ route, navigation }: ProductProps) {
  const { id } = route.params;      // id is a number, checked by the compiler
  navigation.navigate('Search', { query: 'lamps' });
  // navigation.navigate('Serch');  // error: not a route name
  return <Text>Product {id}</Text>;
}

undefined in the list means "no params needed", so navigate('Home') works. A required object type forces the caller to pass it.

Typing the hooks without props

Components that are not screens (a button inside a card, say) cannot receive navigation as a prop. You can register your param list globally once, and every hook then knows your routes:

TypeScript
declare global {
  namespace ReactNavigation {
    interface RootParamList extends RootStackParamList {}
  }
}

After that, useNavigation() returns a navigation object whose navigate is checked against RootStackParamList, with no generics in each component. If you prefer the static configuration style of React Navigation 7, the route names and params are inferred from the config object instead, and you register the root navigator type in the same global interface.

Hooks: useNavigation and useRoute

Passing navigation and route through five levels of components is tedious. The hooks read them from context:

TSX
import { useNavigation, useRoute } from '@react-navigation/native';

function BuyButton() {
  const navigation = useNavigation();
  const route = useRoute();         // the route of the nearest screen
  return (
    <Button
      title={'Buy ' + route.name}
      onPress={() => navigation.navigate('Checkout')}
    />
  );
}

useNavigation returns the navigation object of the nearest screen, which matters in nested navigators (see Tab and Drawer Navigators). useRoute returns that screen's route. Other hooks you will meet are useIsFocused (is my screen on top?), useFocusEffect (run an effect when the screen gains focus and clean up when it loses it) and useNavigationState (read parts of the state tree).

Updating params and returning results

setParams

A screen can change its own params with navigation.setParams({ ... }). The object is merged with the existing params. It is handy for UI state that should survive a reload or a deep link, such as the selected filter, the sort order or the active tab of a screen.

Sending a result back

A common pattern is "open a picker, choose something, come back with the answer". There is no return value from navigate, so the result travels as params to the earlier screen. In React Navigation 7 you go back to that screen with popTo and merge the new params:

TSX
function ComposeScreen({ navigation }: ComposeProps) {
  const [text, setText] = useState('');
  return (
    <>
      <TextInput value={text} onChangeText={setText} />
      <Button
        title="Done"
        onPress={() => navigation.popTo('Home', { draft: text }, { merge: true })}
      />
    </>
  );
}

function HomeScreen({ route }: HomeProps) {
  const draft = route.params?.draft;
  return <Text>{draft ? 'Draft: ' + draft : 'No draft yet'}</Text>;
}

The merge: true option keeps the params the target screen already had. In React Navigation 6 and earlier the same effect was navigation.navigate({ name: 'Home', params: { draft: text }, merge: true }).

Another option for results is a shared store or a callback context, which suits large data. Do not pass callback functions as params: they are not serializable and React Navigation warns about it.

Returning a result to the previous screen (stand-in)

After you press the button, the author is still there (merged) and the draft appears. Without merge, the author param would be gone, because the new params would replace the old object.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where does a screen read the params it was opened with?

    Each screen receives a route prop whose params field holds the object passed to navigate or push. The navigation object is for moving, not reading.
  2. 2. Why should params be small and serializable?

    Navigation state is persisted and mapped to deep links. Pass ids and load fresh data in the target screen.
  3. 3. What does the param list type RootStackParamList: { Home: undefined; Product: { id: number } } mean?

    undefined means no params. A required object type means every navigate call to that route must supply it, and the compiler enforces that.
  4. 4. How do you send a result back to an earlier screen in React Navigation 7?

    navigate returns nothing. The result travels as params to the earlier screen. Functions in params are discouraged because they cannot be serialized.

Summary

  • Pass data with navigate(name, params) and read it from route.params; initialParams supplies defaults.
  • Keep params small, serializable and id-like; load the real data in the target screen.
  • A param list type (Record<route name, params or undefined>) plus NativeStackScreenProps makes every navigate call type-safe; register the list globally to type useNavigation.
  • useNavigation, useRoute, useIsFocused and useFocusEffect give any component access to navigation without prop drilling.
  • setParams merges into the current route; results go back to an earlier screen as params via popTo(..., { merge: true }) (or navigate with merge in version 6).
  • On this site the real library is read-only and the TryIts are reducer stand-ins.

Next, organize bigger apps with Tab and Drawer Navigators.