rchitectures by localizing debugging scope.
- Zero unauthorized cross-imports were recorded in KIP-compliant modules, eliminating cascade failures during refactors.
- Sweet Spot: KIP scales optimally when applied progressively. Simple components remain lean (Level 1), while complex domains naturally expand their private ecosystem without contaminating the global codebase. The architecture shines in teams enforcing strict API boundaries and automated linting.
Core Solution
The KIP (Keep It Private) pattern is an architectural standard for React that enforces Strict Encapsulation at the component level. It treats every componentโregardless of complexityโas an independent micro-domain. Logic, types, utilities, and sub-components (slots) reside exclusively within the component's folder and are explicitly marked as private. External consumption is restricted to a single, controlled gateway.
The Golden Rules of KIP
- The
_ Prefix Means STRICTLY PRIVATE:
Any file starting with an underscore (_) is an internal implementation detail of that specific component (e.g., _hook.ts, _type.ts, _util.ts, _component.tsx). It declares: "I am private. Do not import me directly from outside this folder."
- The
index.ts is The Gate:
The index.ts file acts as the ultimate Gatekeeper (API Boundary). It imports what is necessary from the private _ files and selectively exports them to the rest of the application.
KIP offers Progressive Scaling. You only create the private scopes required to maintain clean code.
Level 1: The Simple Component (e.g., Button)
๐ Button/
โโโ ๐ _type.ts
โโโ ๐ _component.tsx
โโโ ๐ index.ts
Enter fullscreen mode Exit fullscreen mode
Level 2: The Medium Component (e.g., LoginForm)
๐ LoginForm/
โโโ ๐ _hook.ts
โโโ ๐ _util.ts
โโโ ๐ _type.ts
โโโ ๐ _component.tsx
โโโ ๐ index.ts
Enter fullscreen mode Exit fullscreen mode
Level 3: The Complex Component (e.g., DataGrid)
๐ DataGrid/
โโโ ๐ _hook.ts
โโโ ๐ _util.ts
โโโ ๐ _type.ts
โโโ ๐ _store.ts
โโโ ๐ _slots.tsx
โโโ ๐ _component.tsx
โโโ ๐ index.ts
Enter fullscreen mode Exit fullscreen mode
How KIP Solves the React Scaling Crisis:
- True Separation of Concerns (SoC): Eliminates 1,000-line files by distributing logic into specialized micro-files, enabling surgical debugging.
- The
index.ts API Boundary: Components behave like strict NPM packages. index.ts exclusively exports the public contract; internal state transformations and formatting logic remain hidden.
- Zero Global Namespace Pollution: Domain-specific utilities (e.g., table date formatters) stay in
_util.ts. Global src/utils is reserved exclusively for cross-cutting concerns.
- Instant Scalability: Component growth triggers internal expansion rather than architectural rot. The private ecosystem absorbs complexity without leaking dependencies.
Stop treating components as just files. Treat them as domains. Keep It Private.
Pitfall Guide
- Bypassing the
index.ts Gate: Importing _ files directly from parent or sibling components breaks encapsulation and creates hidden coupling. Best Practice: Enforce eslint-plugin-import rules (no-restricted-imports) to block wildcard or direct imports of underscore-prefixed files.
- Over-Encapsulating Global Logic: Placing truly cross-cutting utilities (e.g.,
apiClient, formatCurrency, axios interceptors) inside component _ folders causes duplication and version drift. Best Practice: Reserve src/lib/ or src/utils/ for genuinely global concerns; only domain-specific logic belongs in _ files.
- Neglecting the
_ Prefix Convention: Developers omit the underscore, accidentally exposing internal files to the module resolver. Best Practice: Standardize naming via IDE snippets, Plop generators, or Husky pre-commit hooks that validate file naming against the KIP schema.
- Ignoring Progressive Scaling: Pre-creating all
_ files for a simple component introduces unnecessary boilerplate and cognitive load. Best Practice: Start at Level 1. Only introduce _hook.ts, _store.ts, or _slots.tsx when state complexity, side-effects, or composition requirements explicitly demand it.
- Leaking Internal Types via Props: Exposing
_type.ts interfaces directly in parent component props couples the consumer to internal implementation details. Best Practice: Define stable, public-facing interfaces in index.ts or a dedicated non-private types.ts. Map internal types to public contracts at the gate.
- Circular Dependencies in Barrel Exports: Blindly re-exporting everything from
index.ts can trigger runtime import cycles, especially with complex state stores or slot compositions. Best Practice: Use explicit named exports, validate dependency graphs with madge or depcheck, and avoid re-exporting modules that import the parent component.
Deliverables
- ๐ KIP Architecture Blueprint: A comprehensive visual and textual guide detailing folder topology, boundary definitions, and progressive scaling matrices for React components of varying complexity.
- โ
KIP Compliance Checklist: A PR-ready validation matrix covering underscore naming conventions,
index.ts gate verification, cross-import violation scans, and progressive scaling appropriateness.
- โ๏ธ Configuration Templates: Production-ready ESLint rules (
no-restricted-imports for _ files), TypeScript paths aliases for clean barrel imports, and Vite/Webpack alias configurations to enforce strict module resolution at build time.