Skip to content

Instantly share code, notes, and snippets.

@matterai
Created March 9, 2025 18:56
Show Gist options
  • Save matterai/81124c804325b173096373f37afaa36f to your computer and use it in GitHub Desktop.
Save matterai/81124c804325b173096373f37afaa36f to your computer and use it in GitHub Desktop.
ReactJS Folder Structure and Project Organization Prompt

Structuring Large React Applications:

  • Use feature-based structure approach (grouping by features or domains) and layered structure (grouping by type of file: components, hooks, utils, etc.);
  • Feature (domain) based example: you might have a src/features/ directory with subfolders like auth/, dashboard/, products/, etc. Inside each, you could have files like components/, hooks/, services/, and so on, related only to that feature. Shared resources (like common UI components or utility functions) live in a separate folder (e.g., src/components/ for reusable UI, src/utils/ for utilities);
  • Avoid Deep Nesting: aim for a balance – group things meaningfully, but don’t create one-file-per-folder scenarios either.
  • Use naming conventions for files: Use kebab-case for filenames, and some prefix component files with capital letters;

Managing Reusable Components and Utilities:

  • Centralize Reusable Components: For truly shared components (used in multiple features), maintain a directory for them (e.g., src/components or src/common/components). Examples are buttons, modals, dropdowns, etc. Within that, you might even categorize by type (forms, layout, data-display);
  • Have a central place for utilities (pure functions like formatting, calculation helpers) and services (code that interacts with external APIs). A src/utils folder can contain things like date.ts, string.ts, etc., and a src/services (or api) can contain modules for API calls (e.g., userApi.ts for user-related requests). With TypeScript, you can define interfaces for API responses in the same files or in a adjacent src/types or src/interfaces directory;
  • Shared State (Context/hooks): If you have context providers or custom hooks that are used app-wide (e.g., AuthProvider, useAuth, or a ThemeProvider), consider placing them in a src/context or src/providers directory. If following feature structure, a context that is only used by one feature lives with that feature, whereas a global one (used by many features) lives in a common area;
  • Index Files for Barrel Imports: simplify imports, use index.ts barrel files in directories. For example, in src/utils/, export all utility functions in an index.ts. This way other parts of the app can import { formatDate } from 'utils' (assuming proper path aliases or relative path) instead of deep paths;
  • Be cautious not to create circular dependencies with barrels.

Handling Environment Variables and Configuration:

  • Use environment variables for configuration that differs by environment (development, production, etc.) or for sensitive keys.
  • Avoid magic strings for env variable names, add type definitions for process.env; For example, create a env.d.ts declaring namespace NodeJS { interface ProcessEnv { REACT_APP_API_URL: string; } } so that process.env.REACT_APP_API_URL is recognized by TypeScript;
  • Do Not Commit Secrets: Ensure that your .env files (especially those with real secrets or config) are excluded from version control (add to .gitignore). Instead, use sample env files (like .env.example) to show what keys are needed;
  • Create a config module that reads from env and provides higher-level config. For example, a config.ts that exports API_BASE_URL = process.env.REACT_APP_API_URL ?? '' and other such constants. This centralizes config logic and can enforce required variables (throwing an error if a needed env is missing, to fail fast).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment