<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Howl</title>
    <description>The latest articles on DEV Community by Howl (@howljs).</description>
    <link>https://dev.to/howljs</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1238616%2F27bf6b21-55f4-482b-b091-025e5562513f.jpeg</url>
      <title>DEV Community: Howl</title>
      <link>https://dev.to/howljs</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/howljs"/>
    <language>en</language>
    <item>
      <title>File-system based router in React Native CLI with react-native-auto-route</title>
      <dc:creator>Howl</dc:creator>
      <pubDate>Fri, 22 Dec 2023 19:05:25 +0000</pubDate>
      <link>https://dev.to/howljs/file-system-based-router-in-react-native-cli-with-react-native-auto-route-2d16</link>
      <guid>https://dev.to/howljs/file-system-based-router-in-react-native-cli-with-react-native-auto-route-2d16</guid>
      <description>&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/zSdXkZiJfMs"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;react-native-auto-route&lt;/code&gt; is a file-based router for React Native CLI. It is build on top of &lt;a href="https://reactnavigation.org/"&gt;React Navigation&lt;/a&gt; and &lt;a href="https://docs.expo.dev/router/introduction/"&gt;Expo Router&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It allow you to manage your routes in a simple and intuitive way. When a file is added, removed or renamed, the router is automatically updated. Every screen in your app is automatically deep linkable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1: Install &lt;code&gt;react-native-auto-route&lt;/code&gt;:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn add react-native-auto-route
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Installing peer dependencies:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;If you already have these libraries installed and at the latest version, you can skip this step.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn add react-native-screens react-native-safe-area-context
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you're on a Mac and developing for iOS, you need to install the pods (via &lt;a href="https://cocoapods.org"&gt;Cocoapods&lt;/a&gt;) to complete the linking.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx pod-install ios
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;react-native-screens&lt;/strong&gt; package requires one additional configuration step to properly work on Android devices. &lt;br&gt;
&lt;a href="https://github.com/software-mansion/react-native-screens#android"&gt;https://github.com/software-mansion/react-native-screens#android&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 3: Add Auto Route plugin
&lt;/h3&gt;

&lt;p&gt;Add &lt;code&gt;react-native-auto-route/plugin&lt;/code&gt; plugin to your &lt;code&gt;babel.config.js&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;presets&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="c1"&gt;//...&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="c1"&gt;//...&lt;/span&gt;
      &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-native-auto-route/plugin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 4: Update metro.config.js
&lt;/h3&gt;

&lt;p&gt;Enable &lt;code&gt;unstable_allowRequireContext&lt;/code&gt; in your &lt;code&gt;metro.config.js&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;getDefaultConfig&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;mergeConfig&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@react-native/metro-config&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * Metro configuration
 * https://facebook.github.io/metro/docs/configuration
 *
 * @type {import('metro-config').MetroConfig}
 */&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;transformer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;unstable_allowRequireContext&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;mergeConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;getDefaultConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 5: Update your &lt;code&gt;App.tsx&lt;/code&gt; file
&lt;/h3&gt;

&lt;p&gt;Import &lt;code&gt;react-native-auto-route&lt;/code&gt; and use the &lt;code&gt;RouterRoot&lt;/code&gt; component. It's similar to &lt;code&gt;NavigationContainer&lt;/code&gt; from &lt;code&gt;react-navigation&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;RouterRoot&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-native-auto-route&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;RouterRoot&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 6: Create screens
&lt;/h3&gt;

&lt;p&gt;When a file is created in the screens directory (default is: &lt;code&gt;app&lt;/code&gt;), it will be automatically added to the routes. For example, the following files will create the following routes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;app/index.tsx&lt;/code&gt; matches &lt;code&gt;/&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;app/home.tsx&lt;/code&gt; matches &lt;code&gt;/home&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;app/settings/index.tsx&lt;/code&gt; matches &lt;code&gt;/settings&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;app/[user].tsx&lt;/code&gt; matches dynamic paths like &lt;code&gt;/userId1&lt;/code&gt; or &lt;code&gt;/userId2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;app/(group)/tab1.tsx&lt;/code&gt; matches &lt;code&gt;/tab1&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Supported extensions: &lt;code&gt;.tsx&lt;/code&gt;, &lt;code&gt;.ts&lt;/code&gt;, &lt;code&gt;.jsx&lt;/code&gt;, &lt;code&gt;.js&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can create dynamic routes by using square brackets in the file name. For example, the following files will create the following routes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;app/[user].tsx&lt;/code&gt; matches dynamic paths like &lt;code&gt;/userId1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;app/[user]/[post].tsx&lt;/code&gt; matches dynamic paths like &lt;code&gt;/userId1/postId1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;app/detail/[postId].tsx&lt;/code&gt; matches dynamic paths like &lt;code&gt;/detail/postId1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;app/detail/[...postId].tsx&lt;/code&gt; matches dynamic paths like &lt;code&gt;/detail/postId1/edit&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Routes with higher specificity will be matched before a dynamic route. For example, &lt;code&gt;/detail/post&lt;/code&gt; will match &lt;code&gt;detail/post.tsx&lt;/code&gt; before &lt;code&gt;detail/[id].tsx&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Multiple slugs can be matched in a single route by using the rest syntax (...). For example, &lt;code&gt;app/detail/[...postId].tsx&lt;/code&gt; matches &lt;code&gt;/detail/postId1/edit&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can get params from the route by using the &lt;code&gt;useParams&lt;/code&gt; hook.&lt;/p&gt;

&lt;p&gt;Example: app/[user]/[post].tsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;View&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-native&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;UserPost&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useParams&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;View&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Detail: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; - &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;View&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Documentation: &lt;a href="https://howljs.github.io/react-native-auto-route/docs/getting-started/"&gt;https://howljs.github.io/react-native-auto-route/docs/getting-started/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Github: &lt;a href="https://github.com/howljs/react-native-auto-route"&gt;https://github.com/howljs/react-native-auto-route&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Expo Router: &lt;a href="https://docs.expo.dev/router/introduction/"&gt;https://docs.expo.dev/router/introduction/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;React navigation: &lt;a href="https://reactnavigation.org/docs/getting-started/"&gt;https://reactnavigation.org/docs/getting-started/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>reactnative</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
