-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #42: Add Scalable Application Structure section to README #313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gfgf-brain
wants to merge
4
commits into
piotrwitek:master
Choose a base branch
from
gfgf-brain:brain-fix-42
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6cb7fca
Add solution for #42: [Section] Scalable Application Structure
gfgf-brain be13cfb
Add solution for #42: [Section] Scalable Application Structure
gfgf-brain 5e73092
Fix #42: Add Scalable Application Structure section to README
gfgf-brain bc4ee3c
Fix review feedback on #313: imports, exports, remove scratchpad files
gfgf-brain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2247,6 +2247,157 @@ Higher-Order Components: | |||||||||||||||||
|
|
||||||||||||||||||
| --- | ||||||||||||||||||
|
|
||||||||||||||||||
| --- | ||||||||||||||||||
|
|
||||||||||||||||||
| ## Scalable Application Structure | ||||||||||||||||||
|
|
||||||||||||||||||
| When building large React + Redux + TypeScript applications, a **feature-based** folder structure scales far better than organizing by type. Each self-contained feature module exports its own Redux pieces, making features easy to add, remove, or reuse across projects. | ||||||||||||||||||
|
|
||||||||||||||||||
| ### Folder Layout | ||||||||||||||||||
|
|
||||||||||||||||||
| ``` | ||||||||||||||||||
| src/ | ||||||||||||||||||
| ├── app/ | ||||||||||||||||||
| │ ├── store.ts # configure Redux store, register feature reducers | ||||||||||||||||||
| │ ├── rootReducer.ts # combineReducers from all feature slices | ||||||||||||||||||
| │ └── App.tsx | ||||||||||||||||||
| └── features/ | ||||||||||||||||||
| ├── counter/ | ||||||||||||||||||
| │ ├── index.ts # public API — export only what other features need | ||||||||||||||||||
| │ ├── counterSlice.ts | ||||||||||||||||||
| │ ├── Counter.tsx | ||||||||||||||||||
| │ └── counterSelectors.ts | ||||||||||||||||||
| └── auth/ | ||||||||||||||||||
| ├── index.ts | ||||||||||||||||||
| ├── authSlice.ts | ||||||||||||||||||
| ├── LoginForm.tsx | ||||||||||||||||||
| └── authSelectors.ts | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
|
||||||||||||||||||
| Each feature owns its state, actions, selectors, and UI. Nothing leaks out except through `index.ts`. | ||||||||||||||||||
|
|
||||||||||||||||||
| ### Feature Module Interface | ||||||||||||||||||
|
|
||||||||||||||||||
| ```typescript | ||||||||||||||||||
| // features/counter/counterSlice.ts | ||||||||||||||||||
| import { createSlice, PayloadAction } from '@reduxjs/toolkit'; | ||||||||||||||||||
|
|
||||||||||||||||||
| interface CounterState { | ||||||||||||||||||
| value: number; | ||||||||||||||||||
| status: 'idle' | 'loading' | 'failed'; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| const initialState: CounterState = { value: 0, status: 'idle' }; | ||||||||||||||||||
|
|
||||||||||||||||||
| export const counterSlice = createSlice({ | ||||||||||||||||||
| name: 'counter', | ||||||||||||||||||
| initialState, | ||||||||||||||||||
| reducers: { | ||||||||||||||||||
| increment: (state) => { state.value += 1; }, | ||||||||||||||||||
| decrement: (state) => { state.value -= 1; }, | ||||||||||||||||||
| incrementByAmount: (state, action: PayloadAction<number>) => { | ||||||||||||||||||
| state.value += action.payload; | ||||||||||||||||||
| }, | ||||||||||||||||||
| }, | ||||||||||||||||||
| }); | ||||||||||||||||||
|
|
||||||||||||||||||
| export const { increment, decrement, incrementByAmount } = counterSlice.actions; | ||||||||||||||||||
| export default counterSlice.reducer; | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
|
||||||||||||||||||
| ```typescript | ||||||||||||||||||
| // features/counter/counterSelectors.ts | ||||||||||||||||||
| import type { RootState } from '../../app/store'; | ||||||||||||||||||
|
|
||||||||||||||||||
| export const selectCount = (state: RootState) => state.counter.value; | ||||||||||||||||||
| export const selectStatus = (state: RootState) => state.counter.status; | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
|
||||||||||||||||||
| ```typescript | ||||||||||||||||||
| // features/counter/index.ts — the feature's public API | ||||||||||||||||||
| export { default as counterReducer } from './counterSlice'; | ||||||||||||||||||
| export { increment, decrement, incrementByAmount } from './counterSlice'; | ||||||||||||||||||
| export { selectCount, selectStatus } from './counterSelectors'; | ||||||||||||||||||
| export { Counter } from './Counter'; | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
|
||||||||||||||||||
| ### Registering Features in the Store | ||||||||||||||||||
|
|
||||||||||||||||||
| ```typescript | ||||||||||||||||||
| // app/rootReducer.ts | ||||||||||||||||||
| import { combineReducers } from '@reduxjs/toolkit'; | ||||||||||||||||||
| import { counterReducer } from '../features/counter'; | ||||||||||||||||||
| import { authReducer } from '../features/auth'; | ||||||||||||||||||
|
|
||||||||||||||||||
| const rootReducer = combineReducers({ | ||||||||||||||||||
| counter: counterReducer, | ||||||||||||||||||
| auth: authReducer, | ||||||||||||||||||
| // Add a new feature: just import and add one line here. | ||||||||||||||||||
| // Remove a feature: delete the import and this line. | ||||||||||||||||||
| }); | ||||||||||||||||||
|
|
||||||||||||||||||
| export type RootState = ReturnType<typeof rootReducer>; | ||||||||||||||||||
| export default rootReducer; | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
|
||||||||||||||||||
| ### Enabling / Disabling Features at Runtime | ||||||||||||||||||
|
|
||||||||||||||||||
| For features that should be conditionally loaded (e.g. by role or config flag), inject the reducer dynamically: | ||||||||||||||||||
|
|
||||||||||||||||||
| ```typescript | ||||||||||||||||||
| // app/store.ts | ||||||||||||||||||
| import { configureStore, Reducer, AnyAction, combineReducers } from '@reduxjs/toolkit'; | ||||||||||||||||||
| import rootReducer, { RootState } from './rootReducer'; | ||||||||||||||||||
|
Comment on lines
+2348
to
+2350
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are two issues in this code snippet:
Importing
Suggested change
|
||||||||||||||||||
|
|
||||||||||||||||||
| export type { RootState }; | ||||||||||||||||||
|
|
||||||||||||||||||
| const store = configureStore({ reducer: rootReducer }); | ||||||||||||||||||
|
|
||||||||||||||||||
| /** Inject a reducer after store creation — useful for lazy-loaded feature modules. */ | ||||||||||||||||||
| export function injectReducer(key: string, reducer: Reducer<any, AnyAction>) { | ||||||||||||||||||
| const currentReducers = (store as any).asyncReducers ?? {}; | ||||||||||||||||||
| if (currentReducers[key]) return; // already registered | ||||||||||||||||||
| (store as any).asyncReducers = { ...currentReducers, [key]: reducer }; | ||||||||||||||||||
| store.replaceReducer( | ||||||||||||||||||
| combineReducers({ ...rootReducer, ...(store as any).asyncReducers }) | ||||||||||||||||||
| ); | ||||||||||||||||||
| } | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
|
||||||||||||||||||
| ```typescript | ||||||||||||||||||
| // Lazy-load the analytics feature only for admin users | ||||||||||||||||||
| if (user.isAdmin) { | ||||||||||||||||||
| import('../features/analytics').then(({ analyticsReducer }) => { | ||||||||||||||||||
| injectReducer('analytics', analyticsReducer); | ||||||||||||||||||
| }); | ||||||||||||||||||
| } | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
|
||||||||||||||||||
| ### Rules for Scalable Features | ||||||||||||||||||
|
|
||||||||||||||||||
| | Rule | Why | | ||||||||||||||||||
| |---|---| | ||||||||||||||||||
| | Export only through `index.ts` | Prevents cross-feature implementation coupling | | ||||||||||||||||||
| | No cross-feature imports (use shared/) | Features stay independently removable | | ||||||||||||||||||
| | Selectors live in the feature | Co-located with the state shape they read | | ||||||||||||||||||
| | Feature types are self-contained | Rename/remove without touching other features | | ||||||||||||||||||
|
|
||||||||||||||||||
| ### Shared Utilities | ||||||||||||||||||
|
|
||||||||||||||||||
| Code used by multiple features goes in `src/shared/` or `src/common/`: | ||||||||||||||||||
|
|
||||||||||||||||||
| ``` | ||||||||||||||||||
| src/ | ||||||||||||||||||
| ├── shared/ | ||||||||||||||||||
| │ ├── components/ # Button, Modal, etc. | ||||||||||||||||||
| │ ├── hooks/ # useDebounce, usePrevious, etc. | ||||||||||||||||||
| │ └── utils/ # formatDate, parseAmount, etc. | ||||||||||||||||||
| └── features/ | ||||||||||||||||||
| └── ... | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
|
||||||||||||||||||
| Features may import from `shared/`; they must not import from each other directly. | ||||||||||||||||||
|
|
||||||||||||||||||
| # Contributors | ||||||||||||||||||
|
|
||||||||||||||||||
| Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)): | ||||||||||||||||||
|
|
||||||||||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
combineReducersfunction is used inapp/rootReducer.tsbut is not imported. Please import it from@reduxjs/toolkit(orredux) to avoid a runtimeReferenceError.