← Back to Chapters

React Native Navigation Params & Options

? React Native Navigation Params & Options

? Quick Overview

Navigation parameters and options in React Native allow screens to pass data between each other and dynamically control headers, titles, buttons, and styles. They are commonly used with React Navigation.

? Key Concepts

  • Passing parameters while navigating to another screen
  • Reading route parameters inside destination screen
  • Setting static and dynamic navigation options
  • Updating header title and buttons at runtime

? Syntax / Theory

Navigation params are passed using the navigate() method. Options are configured using options or setOptions().

? Code Example – Passing Params

? View Code Example
// Navigate to Details screen with parameters
navigation.navigate("Details", {
id: 101,
name: "React Native"
});

? Code Example – Receiving Params

? View Code Example
// Access route parameters in destination screen
const { id, name } = route.params;

console.log(id, name);

⚙️ Code Example – Navigation Options

? View Code Example
// Dynamically update header options
React.useEffect(() => {
navigation.setOptions({
title: "Details Screen",
headerStyle: { backgroundColor: "#2563eb" },
headerTintColor: "#fff"
});
}, []);

? Live Output / Explanation

The first screen sends data using navigation params. The destination screen reads those values from route.params. Header title and styles update dynamically using navigation options.

? Interactive Simulator

Use the mock phone below. Enter data on the "Home" screen and see how it is passed to the "Details" screen and how the header options (color) change dynamically.

Home Screen
 

? Home Screen

*This simulates sending data to the next screen.

? Details Screen

Recieved Data (route.params):

// waiting...

Notice the header color changed? This simulates navigation.setOptions.

? Interactive Concept

Think of navigation params like function arguments. Each screen is a function, and params are the input values passed to it.

? Use Cases

  • Passing user ID to profile screen
  • Sending product details to checkout screen
  • Changing header title based on fetched data
  • Enabling or disabling header buttons

✅ Tips & Best Practices

  • Always validate params before using them
  • Prefer setOptions() for dynamic headers
  • Keep params small and serializable

? Try It Yourself

  • Create two screens and pass an object as params
  • Update header title using received params
  • Add a custom header button dynamically