<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ React navigation 5 - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ React navigation 5 - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sun, 24 May 2026 16:31:10 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/react-navigation-5/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Handle Navigation in React Native with react-navigation 5 ]]>
                </title>
                <description>
                    <![CDATA[ By Said Hayani React-navigation is the navigation library that comes to my mind when we talk about navigation in React Native.  I'm a big fan of this library and it's always the first solution I use to handle navigation in React Native. This is in pa... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/introducing-react-navigation-5/</link>
                <guid isPermaLink="false">66d460dd4bc8f441cb6df827</guid>
                
                    <category>
                        <![CDATA[ React navigation 5 ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Android ]]>
                    </category>
                
                    <category>
                        <![CDATA[ iOS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ mobile app development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ programming languages ]]>
                    </category>
                
                    <category>
                        <![CDATA[ react hooks ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React Native ]]>
                    </category>
                
                    <category>
                        <![CDATA[ react-navigation ]]>
                    </category>
                
                    <category>
                        <![CDATA[ technology ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Mon, 23 Mar 2020 19:24:43 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2020/03/react-navigation5-featured-1.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Said Hayani</p>
<p>React-navigation is the navigation library that comes to my mind when we talk about navigation in React Native. </p>
<p>I'm a big fan of this library and it's always the first solution I use to handle navigation in React Native. This is in part becausae it has an awesome and easy API and is very customizable. </p>
<p>I'm writing this article because version 5 just went from beta to stable. It comes with some feature changes and a new API design that provides a simple and different way to declare routes.</p>
<p>In this article, we are going to go through the new APIs and look at ways to use them in our applications.</p>
<blockquote>
<p>Originally published on <a target="_blank" href="https://saidhayani.com/Introducing-react-navigation-5/">saidhayani.com</a></p>
</blockquote>
<h2 id="heading-installing">Installing</h2>
<p>The way you install react-navigation has changed a little bet compared to previous versions (&gt;4.x):</p>
<pre><code class="lang-shell">// &gt; 4.x verions
yarn add react-navigation
</code></pre>
<p>Installing react-navigation 5 will look like this:</p>
<pre><code class="lang-shell">// yarn
yarn add @react-navigation/native
// npm
npm install @react-navigation/native
</code></pre>
<p>The latest versions of react-navigation use many third party library like <a target="_blank" href="https://github.com/software-mansion/react-native-gesture-handler">react-native-gesture-handler</a> for animation and handling transitions. So you always need to install those libraries.</p>
<pre><code class="lang-shell">// yarn
yarn add react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community/masked-view
// npm
npm install react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community/masked-view
</code></pre>
<h2 id="heading-dynamic-screens">Dynamic screens</h2>
<p>The new API introduces dynamism in initializing routes. Previously it was done statically - basically, we had to define our Routes in a config file.</p>
<pre><code class="lang-jsx"><span class="hljs-comment">// @flow</span>
<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;

<span class="hljs-keyword">import</span> { createAppContainer, createSwitchNavigator } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-navigation"</span>;
<span class="hljs-keyword">import</span> { createStackNavigator } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-navigation-stack"</span>;


<span class="hljs-comment">/** ---------Screens----------- */</span>
<span class="hljs-comment">// import LaunchScreen from "../Containers/LaunchScreen";</span>
<span class="hljs-keyword">import</span> HomeScreen <span class="hljs-keyword">from</span> <span class="hljs-string">"../Containers/HomeScreen"</span>;

<span class="hljs-keyword">import</span> ProfileScreen <span class="hljs-keyword">from</span> <span class="hljs-string">"../Containers/ProfileScreen"</span>;
<span class="hljs-keyword">import</span> LoginScreen <span class="hljs-keyword">from</span> <span class="hljs-string">"../Containers/LoginScreen"</span>;






<span class="hljs-keyword">const</span> StackNavigator = createStackNavigator(
  {
    <span class="hljs-attr">initialRouteName</span>: <span class="hljs-string">"Home"</span>
  },
  {
    <span class="hljs-attr">Home</span>: {
      <span class="hljs-attr">screen</span>: HomeScreen
    },
     <span class="hljs-attr">Login</span>: {
      <span class="hljs-attr">screen</span>: LoginScreen,
      <span class="hljs-attr">headerMode</span>: <span class="hljs-string">"none"</span>,

    },
      <span class="hljs-attr">Profile</span>: {
      <span class="hljs-attr">screen</span>: ProfileScreen
    }



);

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> createAppContainer(StackNavigator);
</code></pre>
<p>The new API comes with dynamic components. and made the navigation to be more dynamic.
The new way to declare the Routes will be much like the following.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>
<span class="hljs-keyword">import</span> { SafeAreaView, StyleSheet, View, Text, StatusBar } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-native"</span>

<span class="hljs-keyword">import</span> { NavigationContainer } <span class="hljs-keyword">from</span> <span class="hljs-string">"@react-navigation/native"</span>
<span class="hljs-keyword">import</span> { createStackNavigator } <span class="hljs-keyword">from</span> <span class="hljs-string">"@react-navigation/stack"</span>

<span class="hljs-keyword">const</span> App: <span class="hljs-function">() =&gt;</span> React$Node = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">StatusBar</span> <span class="hljs-attr">barStyle</span>=<span class="hljs-string">"dark-content"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">SafeAreaView</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{styles.containerStyle}</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">AppNavigation</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">SafeAreaView</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  )
}
<span class="hljs-keyword">const</span> Stack = createStackNavigator()
<span class="hljs-keyword">const</span> AppNavigation = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">NavigationContainer</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Stack.Navigator</span> <span class="hljs-attr">initialRouteName</span>=<span class="hljs-string">"home"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Stack.Screen</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"home"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{HomeScreen}</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">Stack.Navigator</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">NavigationContainer</span>&gt;</span></span>
  )
}
<span class="hljs-keyword">const</span> HomeScreen = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">View</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{styles.containerStyle}</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Text</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{styles.title}</span>&gt;</span>Home Screen<span class="hljs-tag">&lt;/<span class="hljs-name">Text</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">View</span>&gt;</span></span>
  )
}
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/03/react-navigation5-demo.gif" alt="react-navigation5-demo" width="600" height="400" loading="lazy"></p>
<p>This new way is dynamic, simpler to use, and is kinda similar to react-router API.</p>
<h2 id="heading-dynamic-options">Dynamic options</h2>
<p>This has been the most requested feature by the community for a long time. I always had issues with the old method (static) and it was really hard to change the navigation behavior dynamically.</p>
<h3 id="heading-the-old-method-gt-lt-4x">The old method =&gt; &lt; 4.x</h3>
<p>With older versions of <a target="_blank" href="https://reactnavigation.org/">react-navigation</a> we had to define static options. And there was no way to change this dynamically.</p>
<pre><code class="lang-js">  <span class="hljs-keyword">static</span> navigationOptions = {
    <span class="hljs-attr">title</span>: <span class="hljs-string">"Sign In"</span>,
    <span class="hljs-attr">header</span>: <span class="hljs-literal">null</span>,
    <span class="hljs-attr">mode</span>: <span class="hljs-string">"modal"</span>,
    <span class="hljs-attr">headerMode</span>: <span class="hljs-string">"none"</span>
  };
</code></pre>
<h3 id="heading-the-new-method-version-5">The new method (version 5)</h3>
<p>React-navigation comes with a dynamic method which is quite simple. We can set the options to any screen using just <code>props</code>.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> AppNavigation = <span class="hljs-function">(<span class="hljs-params">{}</span>) =&gt;</span> {
  <span class="hljs-keyword">let</span> auth = {
    <span class="hljs-attr">authenticated</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-attr">user</span>: {
      <span class="hljs-attr">email</span>: <span class="hljs-string">"user@mail.com"</span>,
      <span class="hljs-attr">username</span>: <span class="hljs-string">"John"</span>,
    },
  }
  <span class="hljs-keyword">let</span> ProfileScreenTitle = auth.authenticated ? auth.user.username : <span class="hljs-string">"Profile"</span>
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">NavigationContainer</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Stack.Navigator</span> <span class="hljs-attr">initialRouteName</span>=<span class="hljs-string">"Home"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Stack.Screen</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"Home"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{HomeScreen}</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Stack.Screen</span>
          <span class="hljs-attr">name</span>=<span class="hljs-string">"Profile"</span>
          <span class="hljs-attr">component</span>=<span class="hljs-string">{ProfileScreen}</span>
          <span class="hljs-attr">options</span>=<span class="hljs-string">{{</span>
            <span class="hljs-attr">title:</span> <span class="hljs-attr">ProfileScreenTitle</span>,
            <span class="hljs-attr">headerTintColor:</span> "#<span class="hljs-attr">4aa3ba</span>",
            <span class="hljs-attr">headerStyle:</span> {
              <span class="hljs-attr">backgroundColor:</span> <span class="hljs-attr">darkModeOn</span> ? "#<span class="hljs-attr">000</span>" <span class="hljs-attr">:</span> "#<span class="hljs-attr">fff</span>",
            },
          }}
        /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Stack.Screen</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"About"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{AboutScreen}</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">Stack.Navigator</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">NavigationContainer</span>&gt;</span></span>
  )
}
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/03/react-navigation-header.png" alt="react-navigation-header" width="600" height="400" loading="lazy"></p>
<p>With dynamic options, I can change the title based on authentication. For example if the user is authenticated, I can set the screen title to be the user’s username, or I can change the backgroundColor for the header. </p>
<p>This is more useful especially if you are using dynamic themes or if you are willing to implement dark mode in your app.</p>
<h2 id="heading-hooks">Hooks</h2>
<p>This is my favorite feature so far, and it’s a time-saver. The new API introduced some custom hooks to perform certain actions.</p>
<p>In previous versions, for example, if I had to get the currentName of the active screen, I had to create some helpers to do that for me pretty much like the following.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getCurrentRouteName</span>(<span class="hljs-params"></span>): <span class="hljs-title">string</span> | <span class="hljs-title">null</span> </span>{
  <span class="hljs-keyword">const</span> tag = <span class="hljs-string">"[getCurrentRouteNameSync] "</span>
  <span class="hljs-keyword">const</span> navState = getStore().getState().nav
  <span class="hljs-keyword">const</span> currentRoute = getActiveRouteState(navState)
  <span class="hljs-built_in">console</span>.log(tag + <span class="hljs-string">" currentRoute &gt; "</span>, currentRoute)
  <span class="hljs-keyword">return</span> currentRoute &amp;&amp; currentRoute.routeName ? currentRoute.routeName : <span class="hljs-literal">null</span>
}
</code></pre>
<p>The hooks API helps me avoid all these things and makes it easier for me to access the Navigation API with one single line using a hook.</p>
<p>Now I can easily get the RouteName using <code>useRoute</code> Hook.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> { useRoute } <span class="hljs-keyword">from</span> <span class="hljs-string">"@react-navigation/native"</span>
<span class="hljs-keyword">const</span> AboutScreen = <span class="hljs-function">(<span class="hljs-params">{ navigation }</span>) =&gt;</span> {
  <span class="hljs-keyword">const</span> route = useRoute()
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">View</span>
      <span class="hljs-attr">style</span>=<span class="hljs-string">{{</span>
        <span class="hljs-attr">justifyContent:</span> "<span class="hljs-attr">space-around</span>",
        <span class="hljs-attr">flex:</span> <span class="hljs-attr">1</span>,
        <span class="hljs-attr">alignItems:</span> "<span class="hljs-attr">center</span>",
      }}
    &gt;</span>
      {/*    Display the RouteName here */}
      <span class="hljs-tag">&lt;<span class="hljs-name">Text</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{styles.title}</span>&gt;</span>{route.name}<span class="hljs-tag">&lt;/<span class="hljs-name">Text</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">View</span>&gt;</span></span>
  )
}
</code></pre>
<p>We can do the same thing with the <code>useNavigationState</code> Hook. It gives us access to the navigation state.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> navigationState = useNavigationState(<span class="hljs-function"><span class="hljs-params">state</span> =&gt;</span> state)
<span class="hljs-keyword">let</span> index = navigationState.index
<span class="hljs-keyword">let</span> routes = navigationState.routes.length
<span class="hljs-built_in">console</span>.log(index)
<span class="hljs-built_in">console</span>.log(routes)
</code></pre>
<p>React-navigation offers other hooks as well, for example:</p>
<ul>
<li><code>useFocuseEffect</code> : a side effect hook that, when the screens are loaded, returns the focused screen</li>
<li><code>useLinking</code>: handles deepLinking</li>
</ul>
<p>I highly recommend that you <a target="_blank" href="https://reactnavigation.org/docs/use-navigation/">check them out</a>.</p>
<h2 id="heading-wrapping-up">Wrapping Up</h2>
<p>The new react-navigation API definitely moves from static to dynamic. It’s a great direction that will absolutely change the way we handle navigation in React Native. Dynamic routes were a major request by react-navigation users, and this new way will help us create a better user navigation experience.</p>
<h3 id="heading-you-can-find-more-content-about-react-native-herehttpssaidhayanicom">You can find more content about <a target="_blank" href="https://saidhayani.com/">React Native here</a></h3>
<blockquote>
<p>Thanks for reading</p>
</blockquote>
<ul>
<li><a target="_blank" href="https://twitter.com/SaidHYN">Twitter</a></li>
<li><a target="_blank" href="https://github.com/hayanisaid">GitHub</a></li>
<li><a target="_blank" href="https://webege.us16.list-manage.com/subscribe?u=311846a57d1e1a666287ad128&amp;id=2b386b2ebb">Join the mail-list</a></li>
</ul>
<blockquote>
<p>Looking for a React Native developer for your project? <strong><a target="_blank" href="mailto:info.said.dev@gmail.com">Hit me up</a></strong>.</p>
</blockquote>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
