Verified Solution

[StackOverflow/reactjs] Recommended folder structure for managing multiple client React projects with shared components?

Sponsored Content
### ROOT CAUSE The user is seeking an organized way to manage multiple React client projects while avoiding code duplication. The challenge lies in maintaining shared components efficiently without tightly coupling them to specific clients, ensuring consistency, and enabling independent development and deployment. ### CODE FIX **Proposed Folder Structure:** ```text /your-project-root ├── shared-components/ # Monorepo for shared components (use Lerna/Yarn workspaces) │ ├── button/ # Individual component library │ │ ├── src/ │ │ │ └── Button.jsx │ │ └── package.json │ ├── utils/ # Shared utilities (e.g., helpers, hooks) │ │ └── package.json │ └── lerna.json # For monorepo management ├── clients/ # Client-specific projects (each as a separate Lerna workspace) │ ├── client-alpha/ │ │ ├── src/ │ │ │ └── App.jsx # Client-specific logic │ │ └── package.json # Depends on shared-components │ ├── client-bravo/ │ └── ... ├── .gitignore └── README.md # Instructions for setup and usage ``` **Implementation Steps:** 1. **Monorepo Setup:** - Use `yarn` or `npm` workspaces to manage `shared-components`, `utils`, etc. - Example `lerna.json`: ```json { "name": "project-root", "version": "0.0.0", "private": true, "workspaces": [ "shared-components/*", "clients/*" ] } ``` - Run `yarn install` in the root to bootstrap dependencies. 2. **Shared Components Library:** - Create individual packages under `shared-components/` (e.g., `shared-components/button`). - Export components via `shared-components/ui/index.js`: ```jsx export { default as Button } from 'shared-components/ui/button'; ``` - Use tools like `tsconfig.json` for TypeScript to enforce consistent typing. 3. **Client Projects:** - Each client is a standalone workspace in `clients/`. - Client `package.json` includes shared dependencies: ```json { "dependencies": { "shared-components": "^1.0.0", "react": "^18.2.0" } } ``` - Client-specific logic (e.g., pages, custom components) resides in `clients/client-alpha/src`. 4. **CI/CD Integration:** - Use GitHub Actions or similar to: - Build and publish shared components on tag releases. - Deploy client projects independently. **Benefits:** - **Reusability**: Shared components are decoupled and versioned. - **Consistency**: Centralized updates prevent drift. - **Scalability**: Add clients or components without structural changes. - **Tooling**: Leverage TypeScript, Storybook for shared components, and Jest for cross-client testing. **Alternative (Non-Monorepo):** If a monorepo is not feasible, structure the filesystem hierarchically: ```text /your-project-root ├── shared/ │ ├── components/ │ │ ├── Button.jsx │ │ └── utils/ │ │ └── helpers.js ├── clients/ │ ├── alpha/ │ │ └── src/ │ │ └── App.jsx │ └── bravo/ └── symlink-or-npm-link.sh # Script to symlink shared components to client node_modules ``` Use `npm link` or `yarn link` to share components, but monorepos are recommended for larger teams.
Deploy on DigitalOcean ($200 Credit)

Related Fixes

[StackOverflow/go] Getting "Cannot query module due to -mod=vendor(Go version in go.mod is at least 1.14 and vendor directory exists.)" error when executing unit test
[rust-lang/rust] Unify labels pointing at the same span in move errors when possible
[facebook/react] [Compiler Bug]: React Compiler silently skips custom hooks whose only hook call is use()