Skip to main content

๐Ÿ’› Best Practices React

The GitHub repository bulletproof-react, which is published as an example of React application architecture and is a great resource for how to structure our application.

In particular:

Directory Structureโ€‹

All code related to web-app should be in the src directory.

Features Directoryโ€‹

src/features/awesome-feature
|
+-- api # exported API request declarations and api hooks related to a specific feature
|
+-- assets # assets folder can contain all the static files for a specific feature
|
+-- components # components scoped to a specific feature
|
+-- hooks # hooks scoped to a specific feature
|
+-- routes # route components for a specific feature pages
|
+-- stores # state stores for a specific feature
|
+-- types # typescript types for TS specific feature domain
|
+-- utils # utility functions for a specific feature
|
+-- index.ts # entry point for the feature, it should serve as the public API of the given feature and exports everything that should be used outside the feature
info

Everything from a feature should be exported from the index.ts file which behaves as the public API of the feature.

You should import stuff from other features only by using:

import { AwesomeComponent } from '@/features/awesome-feature';

Components and Stylingโ€‹

SOLID React Principlesโ€‹

info

Mainly around components Youtube - SOLID React

Abstract shared components into a component libraryโ€‹

For larger projects, it is a good idea to build abstractions around all the shared components. It makes the application more consistent and easier to maintain. Identify repetitions before creating the components to avoid wrong abstractions.

Limit the number of props a component is accepting as inputโ€‹

If your component is accepting too many props you might consider splitting it into multiple components or use the composition technique via children or slots.