Comments (6)
Hey @sbhbenjamin !
it is a good idea to define routes at the application level and then compose the pages with components from different features. I plan to update the demo app codebase to match that setup in the near future.
You can also check out the repository of my book where I covered building a Next.js application with a similar approach.(https://github.com/PacktPublishing/React-Application-Architecture-for-Production)
Basically, you can create as many components as you need in your features and then just compose them in pages.
from bulletproof-react.
You can just start putting everything by default in /app/cart and if you need it somewhere else, you can move it out into features. For example, CartList
belongs to /app/cart
page but what if you need to render the same list when you click on the cart icon in the header? That's when you want to move it outside.
from bulletproof-react.
Hey @alan2207, thanks for the prompt response!
For something like Next.js's new app router, I thought of having the following organisation based on your comments!
/app
directory for the file-based routing/features
where the feature-based organisation lives/shared
which houses components, utilities and types that are shared across multiple features
/src
|-- /app # handles file-based navigation
| |-- /home
| | |-- page.tsx
| |-- /products
| | |-- /[id]
| | | |-- page.tsx
| | |-- page.tsx
| |-- /cart
| | |-- page.tsx
| |-- /about
| | |-- page.tsx
|-- /features # houses feature-based code
| |-- /auth
| | |-- /components
| | | |-- LoginButton.tsx
| | | |-- ProfileDropdown.tsx
| | |-- api.ts
| | |-- types.ts
| |-- /product
| | |-- /components
| | | |-- ProductList.tsx
| | | |-- ProductDetails.tsx
| | |-- api.ts
| | |-- types.ts
| |-- /cart
| | |-- /components
| | | |-- CartList.tsx
| | |-- api.ts
| | |-- types.ts
|-- /shared # houses shared utilities
| |-- /components
| | |-- Header.tsx
| | |-- Footer.tsx
| |-- /utilities
| | |-- formatPrice.ts
| |-- /types
| | |-- common.ts
Along these lines, Next.js offers features such as private folders and route groups, im not quite sure if that would be effective in achieving the strong cohesion in each feature. Do you think they can be helpful in achieving this?
Would love your thoughts on this, and will definitely check out your book โ sounds like an amazing resource!
from bulletproof-react.
Hey @sbhbenjamin ,
In the new app router you could build your components within the private folders in the app folder, especially for components used once at that specific page and nowhere else. Colocation is a great thing for your codebase.
I would probably still consider having the features folder, which in that case could contain only domain-specific code(interfaces, types, utilities, etc.). Think of React/Next.js agnostic stuff
In your example, CartList
would be in app/cart/components/cart-list.tsx
but stuff like CartInterface
or calculatePriceWithVAT
function would go in /features/cart
or wherever you want to organize it...
PS - the book is based on the old pages router but the concepts are pretty similar except now you are able to colocate more files close to your page files...
from bulletproof-react.
@alan2207 I see, so my understanding is that this results in 3 layers of locality:
- Shared layer: where we share code across features
- Feature layer: where we share code within a feature, among different pages
- Colocated layer: where we keep components and immediate dependencies together with the page that needs them
I'm curious, with paths like app/cart/components
and features/cart/components
, could this lead to moments of hesitation for developers about where to look for and place code? Considering that we are doubling up potential locations for similar code!
from bulletproof-react.
Hey @alan2207, that clarifies alot, thank you so much for your insights! :-)
from bulletproof-react.
Related Issues (20)
- Typescript error in form.tsx HOT 2
- React-query + axios errors trigger app crash with react-error-boundary
- Can I get new updated package.json because I got a lot of error to run locally this "bulletproof" HOT 3
- Unused component imported HOT 11
- Can't find module HOT 1
- The requested module _ contains conflicting star exports for the names '$$typeof', '__esModule' with the previous requested module _ HOT 2
- Having issues at Login with React Query
- Failed to load page after registering a user on standalone HOT 1
- Can't start the server HOT 3
- Contexts folder at feature level HOT 5
- [question] How to mock APIs of a "composite" feature? HOT 4
- Reduce or generalize code generated by mutations and queries (API Layer) HOT 2
- Could not start on the latest version of node
- STRUCTURE - Where to store static data? HOT 1
- Logout Functionality Not working Properly - DUC-3
- suggestion for pagination
- Network Error when sign up or login๏ผ HOT 2
- Question : How to make drawer globally? HOT 1
- Where must I put these files? [React, React Native] HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bulletproof-react.