So you manage to finish all of those tutorial and you already run those generate commands, so what's next? Most of the time after generating new react project developers don't know where to put or organize their codes.
After doing some research, trial and error on different react projects, I found the folder structure that works best for me and for my team.
This article is opinionated and you are welcome to adjust it for your own use case. Here's how I organize my React applications:
.
├── README.md
├── package.json
├── tsconfig.json
├── public/
└── src/
├── index.tsx
├── routes.ts
├── api/
├── assets/
├── config/
├── containers/
├── context/
├── types/
├── utils/
└── components/
└── common/
Here's a quick overview for each item or folder.
src/
- Contains all of our react codebase.
index.tsx
- Base react component. If you are not using typescript just change this to 'index.js' or 'index.jsx'.
routes.ts
- App navigation. If you are not using typescript just change this to 'routes.js'.
api/
- Api call related functions.
assets/
- Images, fonts and other static files.
config/
- Config files
containers/
- Smart Components
context/
- React context
types/
- Typescript related files or functions.
utils/
- Helper functions
components/
- Dumb Components
components/common/
- Shared components
You can check this folder structure here.
If you are also using redux on your react application you can check how does it looks here.
I have implemented it with React Native application also, check it here.
Wrap Up
If you have any questions or recommendations, please leave them in the comments below.
Thanks for reading.
Top comments (13)
I used this structure:
How to you think about this structure?
All of components are in functional component, and the component in general is only for HTML and for call a custom hooks to get the business logic.
hey, my dear friend, have you tried concent? that will make state management much easier than u experienced before.
No, I always used Redux.
Now with Toolkit, Redux is very simple.
In the last month I tried RecoilJS (by Facebook).
Thanks for your advice, I will try to us concetjs!
big thx, u just need one min to read its quick start and then know how funny it is 😀
I think this is the same as my setup with just additional folders.
I like this style structure, but what the different point is I put all component to folder
components
here is my folder structure example.
I love this setup also.
That's how I managed my react folder
react-file-structure.surge.sh/
Totally agree, If it doesn't give you frustration I think it's in the right place.
Actually every-time I ended with similar structure as you shown.
sometimes i prefer add a pages floder, the pages components in their floder, common components save in globals components floder
about the code save way.
some util code not too much reuse, how to use save those utils code in your project?
Yup, sometimes I changed my
containers
folder topages
folder and all my pages or screen related components sits there. And all of the reusable components are located insidecomponents
folder. Ex:Too much