DEV Community

Cover image for How to Organize Your Components in React Native: Folder Structure and Project Organization
Paulo Messias
Paulo Messias

Posted on

How to Organize Your Components in React Native: Folder Structure and Project Organization

Organizing components effectively is crucial for the success of any React Native application. A well-structured folder organization not only facilitates maintenance and scalability but also enhances collaboration among developers. In this article, we will delve into best practices for structuring folders and organizing React Native projects, providing a solid foundation for sustainable development.


Why is Folder Structure Important?

Before diving into specific structures, it is essential to understand why folder organization is crucial:

  • Easier Maintenance: Facilitates locating and updating components.
  • Scalability: Allows the project to grow without becoming disorganized.
  • Collaboration: Helps new developers quickly understand the project structure.
  • Code Reusability: Promotes the creation of reusable components, reducing duplication.

Common Folder Structures

There are various approaches to organizing React Native projects, each with its advantages. Let’s explore the most common ones:

1. Type-Based Organization

In this approach, folders are organized by file type or general functionality. It’s one of the most intuitive and easy-to-implement methods, especially for smaller projects.

Example Structure:

src/
  components/
    Button/
      Button.tsx
      Button.styles.ts
      Button.test.tsx
    Header/
      Header.tsx
      Header.styles.ts
  screens/
    Home/
      HomeScreen.tsx
      HomeScreen.styles.ts
    Profile/
      ProfileScreen.tsx
      ProfileScreen.styles.ts
  assets/
    images/
    fonts/
  navigation/
    AppNavigator.tsx
  utils/
    helpers.ts
    constants.ts
Enter fullscreen mode Exit fullscreen mode

Advantages:

  • Easy to understand and implement.
  • Ideal for small to medium projects.

Disadvantages:

  • Can become disorganized as the project grows.
  • Difficulty in locating specific components in larger projects.

2. Domain-Based Organization

Here, folders are organized by specific functionalities or domains of the application. This approach is highly modular and scalable, making it ideal for larger and more complex projects.

Example Structure:

src/
  auth/
    components/
      LoginForm.tsx
      SignupForm.tsx
    screens/
      LoginScreen.tsx
      SignupScreen.tsx
    services/
      authService.ts
  dashboard/
    components/
      DashboardHeader.tsx
      DashboardCard.tsx
    screens/
      DashboardScreen.tsx
    services/
      dashboardService.ts
  common/
    components/
      Button/
        Button.tsx
        Button.styles.ts
      Modal/
        Modal.tsx
        Modal.styles.ts
  navigation/
    AppNavigator.tsx
  assets/
    images/
    fonts/
  utils/
    helpers.ts
    constants.ts
Enter fullscreen mode Exit fullscreen mode

Advantages:

  • High modularity, facilitating scalability.
  • Better isolation of functionalities, reducing coupling.
  • Facilitates code reuse within each domain.

Disadvantages:

  • Can be more complex to set up initially.
  • Requires discipline to maintain consistency as the project grows.

Combining Approaches

In many cases, a combination of type-based and domain-based approaches may be the most effective. For example, you might organize common components by type within a common/ folder while grouping specific functionalities by domain.

Example Combined Structure:

src/
  common/
    components/
      Button/
        Button.tsx
        Button.styles.ts
      Modal/
        Modal.tsx
        Modal.styles.ts
  features/
    auth/
      components/
        LoginForm.tsx
        SignupForm.tsx
      screens/
        LoginScreen.tsx
        SignupScreen.tsx
      services/
        authService.ts
    dashboard/
      components/
        DashboardHeader.tsx
        DashboardCard.tsx
      screens/
        DashboardScreen.tsx
      services/
        dashboardService.ts
  navigation/
    AppNavigator.tsx
  assets/
    images/
    fonts/
  utils/
    helpers.ts
    constants.ts
Enter fullscreen mode Exit fullscreen mode

Advantages:

  • Combines the modularity of domain-based organization with the clarity of type-based organization.
  • Facilitates reuse of common components across different functionalities.

Detailed Breakdown of Folder Structure

Let’s delve a bit deeper into the main folders to understand their function and how to use them effectively.

1. components/

This folder contains reusable components that can be used in various parts of the application. Each component should have its own subfolder containing the component file, styles, and tests.

Example:

components/
  Button/
    Button.tsx
    Button.styles.ts
    Button.test.tsx
  Header/
    Header.tsx
    Header.styles.ts
Enter fullscreen mode Exit fullscreen mode

Best Practices:

  • Atomic Components: Create small, focused components like buttons, inputs, etc.
  • Isolated Styles: Keep styles in separate files for easier maintenance.
  • Testing: Include test files to ensure component quality.

2. screens/

This folder contains the screens or pages of the application. Each screen typically corresponds to a route or major section of the application.

Example:

screens/
  Home/
    HomeScreen.tsx
    HomeScreen.styles.ts
  Profile/
    ProfileScreen.tsx
    ProfileScreen.styles.ts
Enter fullscreen mode Exit fullscreen mode

Best Practices:

  • Single Responsibility: Each screen should be responsible for a single functionality or view.
  • Internal Components: Create a subfolder within each screen for components specific to that screen.

3. features/

When using domain-based organization, the features/ folder groups all files related to a specific functionality.

Example:

features/
  auth/
    components/
      LoginForm.tsx
      SignupForm.tsx
    screens/
      LoginScreen.tsx
      SignupScreen.tsx
    services/
      authService.ts
  dashboard/
    components/
      DashboardHeader.tsx
      DashboardCard.tsx
    screens/
      DashboardScreen.tsx
    services/
      dashboardService.ts
Enter fullscreen mode Exit fullscreen mode

Best Practices:

  • Isolated Logic: Keep functionality-specific logic within its own folder.
  • Internal Reuse: Components within features/ can be reused only within that domain to avoid conflicts.

4. common/

Components and utilities used across multiple functionalities should reside in the common/ folder.

Example:

common/
  components/
    Button/
      Button.tsx
      Button.styles.ts
    Modal/
      Modal.tsx
      Modal.styles.ts
Enter fullscreen mode Exit fullscreen mode

Best Practices:

  • Global Reuse: Components here should be generic enough to be used anywhere in the app.
  • Documentation: Document how to use these components to facilitate their usage by other developers.

5. assets/

Holds all static resources such as images, fonts, icons, etc.

Example:

assets/
  images/
    logo.png
    background.jpg
  fonts/
    OpenSans-Regular.ttf
    OpenSans-Bold.ttf
Enter fullscreen mode Exit fullscreen mode

Best Practices:

  • Type-Based Organization: Separate resources by type for easier location.
  • Clear Naming: Use descriptive names for files to avoid confusion.

6. navigation/

Contains all navigation configurations for the application, such as stacks, tabs, and navigators.

Example:

navigation/
  AppNavigator.tsx
  AuthNavigator.tsx
  DashboardNavigator.tsx
Enter fullscreen mode Exit fullscreen mode

Best Practices:

  • Modular Navigation: Separate navigators by functionality or user flow.
  • Centralization: Keep navigation centralized for easier adjustments and maintenance.

7. utils/

Includes utility functions, constants, and helpers that are used throughout the application.

Example:

utils/
  helpers.ts
  constants.ts
  validators.ts
Enter fullscreen mode Exit fullscreen mode

Best Practices:

  • Code Reuse: Use this folder for functions that can be reused in multiple places.
  • Documentation: Document complex functions to facilitate understanding and use.

Conclusion

A well-organized folder structure in React Native projects is critical for long-term success and maintainability. By adopting a structured approach—whether it’s type-based, domain-based, or a combination—you set a strong foundation for scalable and efficient development. Remember to also adhere to best practices such as consistent naming, modularization, and thorough documentation to further enhance your project's quality.

Top comments (1)

Collapse
 
luizrebelatto profile image
Luiz Gabriel

Nice content, very interesting!!