DEV Community

Discussion on: How I structure my mid-size NextJS projects

Collapse
 
hisuwh profile image
Henry Ing-Simmons

Organising code by feature is the way forward. I shudder whenever I see /components...

Collapse
 
robinlloyd profile image
Robin

Do you have any examples of organising code by feature? I'm curious as to how that goes as I very much have a similar approach to this article at the moment, but I'm definitely open to trying different methods.

Collapse
 
robinlloyd profile image
Robin

Ignore me, I should learn to google things more haha. I'm guessing you mean something along the lines of this right? reactjs.org/docs/faq-structure.htm...

That's interesting, I'll have to try that approach in my next project. It can be super frustrating sifting through a huge components directory so having one common directory for anything shared and everything else being at a page or feature level could look more organised.

Thread Thread
 
hisuwh profile image
Henry Ing-Simmons

Yh this is it exaxtly. I haven't done a lot of next.js so I'm not sure how this works with pages, but essentially you have a folder for a feature, say like "user" then within that you can have your UserProfile component, your EditProfile component and whatever else all conveniently co-located. I also a proponent of having your tests next to their subject files so they're easy to update.

If these folders grow too large you can break them down into sub features.

Collapse
 
josemukorivo profile image
Joseph Mukorivo

Seems to be maintainable, but how do you deal with shared and small components like buttons?

Collapse
 
hisuwh profile image
Henry Ing-Simmons

As Robin has alluded to you can have a common folder. I in fact have a fairly large common folder, broken down into sub-features - e.g. common/buttons. I also don't like the index.js approach as someone else has mentioned so I would have common/buttons/SaveButton.js, common/buttons/CancelButton.js etc. Then one index.js file in buttons to export these.

For common you can even then add a webpack alias so you can import these from anywhere without lots of ../../, e.g. import { SaveButton } from "@common/buttons";