Skip to main content

Project Structure

Setup typescript for easy import#

During your journey writing clean and reusable code, for sure you are going to use import x for "file" statement and as you code source grow you will find yourself writing import some wired import statement like the following:

import foo from "../.../../folder/foo";

The best way to make your code clean in such cases is to use absolute import.

To start using absolute import in your project you need to update baseUrl config inside the tsconfig.json configuration file to ./src.

Aso you will need to use module-resolver babel plugin to update resolver root folder for babel like the following:

plugins: [    'module:react-native-dotenv',    [      'module-resolver',      {        root: ['./src'],        extensions: [          '.ios.ts',          '.android.ts',          '.ts',          '.ios.tsx',          '.android.tsx',          '.tsx',          '.jsx',          '.js',          '.json',        ],      },    ],  ],

Project structure:#

The is a real folder tree of a React Native application src folder we Recommend to follow:

.โ”œโ”€โ”€ index.tsxโ”œโ”€โ”€ apiโ”‚ย ย  โ”œโ”€โ”€ usePosts.tsxโ”‚ย ย  โ””โ”€โ”€ index.tsxโ”œโ”€โ”€ coreโ”‚ย ย  โ”œโ”€โ”€ Authโ”‚ย ย  โ”œโ”€โ”€ I18nโ”‚ย ย  โ””โ”€โ”€ index.tsxโ”œโ”€โ”€ navigationโ”‚ย ย  โ”œโ”€โ”€ AuthNavigator.tsxโ”‚ย ย  โ”œโ”€โ”€ NavigationContainer.tsxโ”‚ย ย  โ”œโ”€โ”€ RootNavigator.tsxโ”‚ย ย  โ”œโ”€โ”€ TabNavigator.tsxโ”‚ย ย  โ””โ”€โ”€ index.tsxโ”œโ”€โ”€ screensโ”‚ย ย  โ”œโ”€โ”€ Homeโ”‚ย ย  โ”œโ”€โ”€ Loginโ”‚ย ย  โ””โ”€โ”€ index.tsxโ”œโ”€โ”€ translationsโ”‚ย ย  โ””โ”€โ”€ en.jsonโ”‚ย ย  โ””โ”€โ”€ ar.jsonโ””โ”€โ”€ ui    โ”œโ”€โ”€ Button.tsx    โ”œโ”€โ”€ ErrorHandler    โ”œโ”€โ”€ Form.tsx    โ”œโ”€โ”€ Icon.tsx    โ”œโ”€โ”€ Input.tsx    โ”œโ”€โ”€ Screen.tsx    โ”œโ”€โ”€ Text.tsx    โ”œโ”€โ”€ Toast.tsx    โ”œโ”€โ”€ View.tsx    โ”œโ”€โ”€ index.tsx    โ””โ”€โ”€ theme
  • ui : In the ui folder we create our design system and all common components and theme.
  • core : This folder is useful for any Provider and general concepts( a folder that you can easily copy-paste between project). here, we put our Auth, localization...
  • translations : translations files
  • navigation: Any Navigation-related components ( Stacks, Tabs ....)
  • api : we create hooks to call our API.
  • screens: A folder for our App screens (a folder for every screen)

You can think of this as modules and every module have an index file that exports all components and functions from the entire folder, which makes importing components very clean.

Example of a screen Home:

import React from "react";import { Button, Screen, Text, View } from "ui";import { API } from "@env";import { translate, useAuth } from "core";
export const Home = () => {  const { signOut } = useAuth();  return (    <Screen>      <View flex={1} justifyContent="center">        <Text variant="header" textAlign="center">          {translate("name")}        </Text>        <Text variant="body" textAlign="center">          This is An ENV Var : {API}        </Text>        <Button label="LogOut" onPress={signOut} />      </View>    </Screen>  );};

๐Ÿ‘‰ https://github.com/obytes/react-native-template-obytes/tree/master/template

Last updated on by Youssouf El Azizi