DEV Community

Discussion on: Better file structure in React projects

Collapse
 
itays123 profile image
Itay Schechner

I believe foldering by context (I.e home page, profile page, etc.) instead of foldering by role (components, hooks, etc.) is a much more convinient way to structure your folders, because when another developer looks at your src folder they know exactly what the project is made for.

Collapse
 
victorocna profile image
Victor Ocnarescu • Edited

I choose foldering by context only on my React components, which are composable by their nature. I do this whenever I can extract a prefix or a suffix from the filename (see AddClientForm.jsx in the folder Forms)

Collapse
 
itays123 profile image
Itay Schechner

That's really cool

Collapse
 
victorocna profile image
Victor Ocnarescu

However, I use foldering by role on pretty much anything else because a function or a hook is more generic, it does not have to depend on some context.

Another benefit of foldering by role. Whenever I want to update something related to a hook, I always know where to go: somewhere in the hooks folder

Collapse
 
itays123 profile image
Itay Schechner

Personally, the vast majority of the hooks I write are written because I wanted to extract some logic from the UI component using it, and for that reason I place in in the same folder as the component.

For hooks used in multiple places, I usually create a helpers folder.

In a project where you have hooks that don't fall in neither of these two categories, foldering by role would be indeed the best choice. But I can't think of many examples that don't fall in one of these categories.