Skip to content
Back to skills

React Rules

ASecurity

Team conventions for React components and hooks (React 19): namespace import, ternary conditional rendering, stable list keys, no manual memoization under the React Compiler and its four exceptions, hooks at the top level, one context per file, hooks folder, a stories file for every component. Use when writing, fixing or reviewing a React component or hook. Background knowledge that agents preload; not a command.

  • 2 stars
  • 0 votes
  • 0 copies
  • 4 views
  • Added November 17, 2026
developmenttypescriptgoreactnextjsapi

Works with

  • cli
  • api

Security analysis

A100/100

Pro scans all 2 files and shows the line behind each finding

Scanned December 1, 2026

npx -y skills add JanSzewczyk/claude-plugins --skill react-rules --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of React Rules?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for React Rules
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/janszewczyk-react-rules/badge)](https://www.skillsdirectory.com/skills/janszewczyk-react-rules)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: react-rules
description: "Team conventions for React components and hooks (React 19): namespace import, ternary conditional rendering, stable list keys, no manual memoization under the React Compiler and its four exceptions, hooks at the top level, one context per file, hooks folder, a stories file for every component. Use when writing, fixing or reviewing a React component or hook. Background knowledge that agents preload; not a command."
user-invocable: false
---

<!-- generated by knowledge.mjs from 10 note(s) (sha256 e204c2e2be11); edit the notes in knowledge/, then run build -->

# React rules

Apply these rules to every React component and hook you write or review. TypeScript style is in `typescript-rules`; server and client component boundaries in Next.js are in `nextjs-rules`. The first section is always in force; open a rule from the table when the task touches it.

## Always apply (critical, high)

### Adopt the React Compiler and stop hand-writing memoization

_high · typescript, react, react-compiler_

**Decision.** Run the React Compiler on the whole app (`compilationMode: "infer"`, the default) and write components as plain code: no `useMemo`, `useCallback` or `memo` except the four documented cases (a callback for a non-React library, a context provider value, a measured expensive computation, a subscription handler), each with a JSDoc comment. Lint with the `recommended` preset of `eslint-plugin-react-hooks`, which carries the compiler rules, and fix what it reports instead of disabling rules. Call hooks only at the top level, since the compiler skips code that breaks the Rules of Hooks.

### Key list items by a stable id from the data, never by the array index

_high · typescript, react_

**Rule.** Give each element rendered in `map` a `key` taken from the data: an `id`, a slug or another unique field. When the data has none, attach a generated id when the data is created or loaded, not while rendering.

**Why.** With index keys, reordering, filtering or inserting rows makes React reuse the wrong component, so a row keeps another row's input text or open state; a key generated during render (`crypto.randomUUID()` inside `map`) is worse, because it remounts every row on every render.

**Correct:**

```tsx
function TodoList({ todos }: TodoListProps) {
  return (
    <ul>
      {todos.map((todo) => (
        <TodoItem key={todo.id} todo={todo} />
      ))}
    </ul>
  );
}
```

### Leave memoization to the React Compiler; memoize by hand only in four documented cases

_high · typescript, react, react-compiler_

**Rule.** Write derived values, event handlers and components as plain code and let the React Compiler memoize them. Do not add `React.useMemo`, `React.useCallback` or `React.memo` unless the case is one of the four exceptions below, and then put a JSDoc comment on it that names the exception.

**Why.** Hand-written memoization in compiled code is noise with dependency arrays that drift out of date, so a stale `useCallback` quietly keeps calling an old prop; the compiler already memoizes the same values with correct dependencies.

**Correct:**

```tsx
function ProductList({ products, sortBy, onSelect }: ProductListProps) {
  const sorted = [...products].sort(byField(sortBy));
  return <List items={sorted} onSelect={(id) => onSelect(id)} />;
}
```

**Exceptions.** Memoize by hand, with a JSDoc comment stating which case applies, only for:

1. **A callback handed to code outside React** (a map, chart or editor library that keeps the reference): `/** Stable reference: the map library re-binds its click handler when this changes. */`
2. **A context provider value** that is an object or function, so consumers do not re-render on every provider render: `/** Context value: keeps every consumer from re-rendering when the provider does. */`
3. **A computation measured as expensive** in the React Profiler (a render over 16 ms, or a computation over 100 ms that runs often): `/** Measured: ~150 ms for 10k rows in the Profiler. */`
4. **A subscription handler** passed to an effect that subscribes to an external store or socket, so it does not resubscribe on every render: `/** Stable handler: prevents resubscribing to the channel on every render. */`

### Render conditionally with a ternary that returns null, never with &&

_high · typescript, react_

**Rule.** Write every conditional in JSX as `condition ? <Element /> : null`, including fragments and inline text. Do not use the `&&` short-circuit.

**Why.** With `&&`, a numeric left side that is `0` (or `NaN`) is rendered as-is, so an empty list shows a stray "0" on the page; the ternary always renders exactly the element or nothing. Banning `&&` outright, even with a boolean left side, is a team style choice that keeps the rule mechanical to follow and to lint.

**Correct:**

```tsx
function OrderList({ orders, error }: OrderListProps) {
  return (
    <section>
      {orders.length > 0 ? <List items={orders} /> : null}
      {error ? <p className="text-error">{error}</p> : null}
    </section>
  );
}
```

## Read when the task touches it

| Impact | Rule | Applies to |
| --- | --- | --- |
| medium | [Ship every component with a stories file beside it](references/rules.md#ship-every-component-with-a-stories-file-beside-it) — Each {component}.tsx gets a {component}.stories.tsx in the same folder, created in the same change. | typescript, react, storybook |
| medium | [Keep each context with its provider and consumer hook in one name.context.tsx file](references/rules.md#keep-each-context-with-its-provider-and-consumer-hook-in-one-namecontexttsx-file) — One React.createContext per context/{name}.context.tsx, together with its Provider component and its use{Name} hook. | typescript, react |
| medium | [Put a custom hook that creates no context in hooks/use-{name}.ts](references/rules.md#put-a-custom-hook-that-creates-no-context-in-hooksuse-namets) — Standalone client logic, or a hook built on a context hook, goes to hooks/; a context-defining hook stays in context/. | typescript, react |
| medium | [Call hooks only at the top level of a component or custom hook](references/rules.md#call-hooks-only-at-the-top-level-of-a-component-or-custom-hook) — No hook inside a condition, loop, nested function or after an early return; React.use is the one exception. | typescript, react |
| medium | [Fix React Compiler lint diagnostics instead of switching the rules off](references/rules.md#fix-react-compiler-lint-diagnostics-instead-of-switching-the-rules-off) — The recommended preset carries the React Compiler rules; a disabled rule hides code the compiler silently skips. | typescript, react, react-compiler |
| medium | [Import React as a namespace and reach every API through it](references/rules.md#import-react-as-a-namespace-and-reach-every-api-through-it) — import * as React from "react", then React.useState, React.cache; no default import, no named imports. | typescript, react |

Files in this skill

  • SKILL.md6.8 KB
  • references/rules.md7.6 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.