Why and How to implement global paths in React Native.

Cover Image for Why and How to implement global paths in React Native.
Rajeshkumar S
Rajeshkumar S
18 days ago
webappmobiledevelopmentandroidJavaScriptReactReact NativeiOScross-platform

Introduction

When you start a new React Native project, you might notice that the import paths can get quite long and messy. This can make it hard to keep track of where your files are located and can lead to errors when moving files around. One way to solve this problem is by using global paths in your project. Global paths allow you to define a base path for your project and import files relative to that base path. This can make your imports cleaner and easier to read, and can help you organize your project more effectively.

Why use global paths?

There are several benefits to using global paths in your React Native project. Some of the main benefits include:

  • Cleaner imports: Global paths allow you to import files using a shorter and more readable syntax. This can make your code easier to understand and maintain.
  • Easier refactoring: When you use global paths, you can move files around in your project without having to update all of your import statements. This can save you time and reduce the risk of errors.
  • Better organization: Global paths can help you organize your project more effectively by grouping related files together. This can make it easier to find and work with your files.

The relative path are like the following:

import Button from '../../components/Button';
import Text from '../../components/Text';
import { colors } from '../../styles';

How to set up global paths in your React Native project

My project structure is like the following:

NotesApp
    node_modules
    src
      components
      routes
      screens
      service
      utils
    App.tsx
    package.json
    tsconfig.json

Setting up global paths in your React Native project is relatively simple. Here's how you can do it:

  1. Install babel-plugin-module-resolver and @react-native/typescript-config packages in your project. These packages will allow you to define global paths in your project and tell the compiler where to look for files.
    npm install --save-dev babel-plugin-module-resolver @react-native/typescript-config
    //or
    yarn add --dev babel-plugin-module-resolver @react-native/typescript-config
  1. Create a tsconfig.json file in the root of your project. This file will define the base path for your project and tell the compiler where to look for files.
tsconfig.json
{
    "extends": "@react-native/typescript-config/tsconfig.json",
    "compilerOptions":{
        "baseUrl": "src",
        "types": ["@firebase/firestore-types"],
        "paths": {
            "@components": ["components/index"],
            "@screens/*": ["screens/*"],
            "@utils/*": ["utils/*"],
            "@routes/*": ["routes/*"]
        }
    }
}
  1. Update your balel.config.js file to use the tsconfig.json file you just created. This will tell the compiler to use the settings in the tsconfig.json file when building your project.
babel.config.js
module.exports = {
    presets: ['module:@react-native/babel-preset'],
    plugins: [
        [
            'module-resolver',
        {
            root: ['./src'],
            extensions: ['.js', '.ts', '.jsx', '.tsx', '.json'],
            alias: {
                '@components': './src/components/index',
                '@screens': './src/screens',
                '@utils': './src/utils',
                '@routes': './src/routes',
            },
        },
        ],
    ],
};
  1. Now you can start using global paths in your project. For example, if you want to import the Button component from the components directory, you can do so like this:

    import Button from '@components/Button';

    This will import the Button component using the global path @components, which is defined in the tsconfig.json file. You can use global paths in the same way for other directories in your project.

    By setting up global paths in your React Native project, you can make your imports cleaner and easier to read, and help you organize your project more effectively. This can save you time and reduce the risk of errors when working on your project.

    I hope this article helps you understand why and how to implement global paths in your React Native project. If you have any questions or comments, please feel free to leave them below. Thank you for reading!

    To know more about the React Native project, you can follow our Blog.

    Happy coding!