A robust architectural pattern for React that enforces strict encapsulation, eliminates "God Files," and treats every component as an independent micro-domain.
โ The Problem
As React applications grow, components tend to follow two destructive paths:
- The God File: A single
.tsxfile inflates to 1000+ lines containing UI, hooks, interfaces, and utilities. - Leaky Internals: Developers split the file, but put component-specific utilities and types in global folders (e.g.,
src/utils), polluting the global namespace.
โ The KIP Solution
KIP (Keep It Private) solves this by enforcing Separation of Concerns (SoC) inside the component's folder while keeping those concerns strictly hidden from the rest of the app.
The Golden Rules
_means PRIVATE: Any file with an underscore prefix (e.g.,_hook.ts) is strictly internal. Direct imports from outside the folder are forbidden.index.tsis THE GATE: This is the only API boundary. It gathers the internal pieces and explicitly exports only what the outside world is allowed to consume.
๐ How KIP Solves the React Scaling Crisis:
- True Separation of Concerns (SoC): No more 1000-line files. Your logic is cleanly separated into specialized micro-files, making debugging incredibly focused.
- The
index.tsAPI Boundary: Your component acts like a strict NPM package.index.tsONLY exports what the rest of the application needs to know. The dirty work remains hidden. - Zero Global Namespace Pollution: That weird utility function that formats a specific table date? It stays in
_util.ts. Your globalsrc/utilsfolder is now strictly reserved for truly global helpers. - Instant Scalability: When a component grows, it doesnโt rot. It simply utilizes its private ecosystem.
๐ Progressive Scaling
KIP is highly flexible. It scales progressively based on the component's complexity. You only use the files you need.
Level 1: Simple Component (Button)
๐ src/components/Button/
โโโ ๐ _type.ts # ButtonProps, VariantTypes
โโโ ๐ _component.tsx # Actual JSX
โโโ ๐ index.ts # export { Button } from './_component';
Level 2: Medium Component (LoginForm)
๐ src/components/LoginForm/
โโโ ๐ _hook.ts # useLoginForm()
โโโ ๐ _util.ts # validateEmail(), formatPassword()
โโโ ๐ _type.ts # LoginFormProps, FormState
โโโ ๐ _style.ts # Component styles
โโโ ๐ _component.tsx # The UI tying it together
โโโ ๐ index.ts # Gate
Level 3: Complex Component (DataGrid)
๐ src/components/DataGrid/
โโโ ๐ _hook.ts # Data fetching, sorting logic
โโโ ๐ _util.ts # Data transformers
โโโ ๐ _type.ts # Complex generic types
โโโ ๐ _store.ts # Local component state (Zustand/Context)
โโโ ๐ _style.ts # Component styles
โโโ ๐ _slots.tsx # Internal sub-components (Row, Cell, Header)
โโโ ๐ _component.tsx # Main Wrapper
โโโ ๐ index.ts # Gate
๐ Framework Agnostic
While the examples in this repository use React and TypeScript, the KIP Pattern is completely framework-agnostic. It is an architectural principle of encapsulation that can be applied to almost any modern tech stack.
The core formula of _scope.ext (private internals) + index.ext (public gatekeeper) is universal. Here is how KIP translates to other ecosystems:
Vue.js
Instead of dumping everything into a massive .vue file, split complex logic:
UserProfile/
โโโ _types.ts
โโโ _useProfile.ts (Composable)
โโโ _ProfileAvatar.vue (Internal component)
โโโ index.vue (The Gate - public component)
Node.js / Backend (Express / NestJS)
Keep your domain logic cleanly separated and hide internal database queries or helper functions:
AuthModule/
โโโ _utils.ts (Hash logic, token generation)
โโโ _queries.ts (DB interactions)
โโโ _types.ts (Internal DTOs)
โโโ index.ts (Exports only the Auth Controller or public Service methods)
Angular
Angular already encourages modularity, but KIP can be applied inside feature folders to hide internal pipes, directives, or sub-components that shouldn't be imported by other modules.
The Universal Rule
No matter the language or framework (JavaScript, Python, Go, etc.), the rule remains the same: If a file starts with _, it belongs to that folder. Do not import it from the outside.
๐ Enforcing KIP in Your Team
To ensure developers don't bypass the index.ts Gate, you can enforce KIP using ESLint no-restricted-imports rules to ban */_*.ts imports from outside their own directories.
๐ ๏ธ Usage / Boilerplate
Clone this repository to see the KIP pattern implemented in a modern Vite + React + TypeScript + ESLint environment.
bash git clone https://github.com/Miladxsar23/kip-pattern.git cd kip-pattern-react npm install npm run dev
๐ค Contribution
Have ideas to make KIP even better? Feel free to open an issue or submit a PR! Let's build cleaner, more encapsulated React apps together.






















