Code Reference

File structure

React · Reference cheat sheet

File structure

React · Reference cheat sheet


📋 Overview

Organize by feature or route, keep components small, and colocate styles/tests. Exact layout depends on Vite, Next.js, or CRA — patterns below are common.

🔧 Core concepts

  • Entrymain.tsx / index.tsx mounts the root.
  • App shellApp.tsx for providers, layout, routes.
  • Featuresfeatures/auth/, features/cart/ with local components.
  • Sharedcomponents/, hooks/, lib/, types/.
  • Pages/routes — framework-specific (pages/, app/, routes/).

💡 Examples

src/
  main.tsx
  App.tsx
  components/
    Button.tsx
    Button.module.css
  features/
    todos/
      TodoList.tsx
      useTodos.ts
      api.ts
  hooks/
    useMediaQuery.ts
  lib/
    http.ts
  pages/
    HomePage.tsx
    TodoPage.tsx
// main.tsx
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import { App } from "./App";

createRoot(document.getElementById("root")!).render(
  <StrictMode>
    <App />
  </StrictMode>,
);

⚠️ Pitfalls

  • Deep ../../../ imports — use path aliases (@/).
  • Giant components/ dumping ground — prefer feature folders.
  • Mixing business logic into presentational files without clear boundaries.

On this page