Cursor rules for React with TypeScript: typed function components, React 19 ref-as-prop, hooks discipline, state ownership, error boundaries and measured performance work.
.cursor/rules/react-typescript-best-practices.mdc. Downloaded files need to be placed in that repository path.---
description: "Cursor rules for React with TypeScript: typed function components, React 19 ref-as-prop, hooks discipline, state ownership, error boundaries and measured performance work."
globs: "**/*"
alwaysApply: false
---
Apply these rules when writing React components in TypeScript.
## Components
- Write function components with typed props. Use `interface` or `type` consistently with the codebase; a `type` is needed for unions and intersections.
- Follow the project's export convention. Named exports are easier to find and rename; frameworks that require a default export (for example Next.js `page.tsx` and `layout.tsx`) are the exception.
- Extend native element props instead of re-declaring them, so `disabled`, `aria-*` and event handlers pass through:
```tsx
import type { ComponentProps } from 'react';
type ButtonProps = ComponentProps<'button'> & {
variant?: 'primary' | 'secondary';
};
export function Button({ variant = 'primary', type = 'button', className, ...props }: ButtonProps) {
return (
<button
type={type}
className={['btn', `btn-${variant}`, className].filter(Boolean).join(' ')}
{...props}
/>
);
}
```
- In React 19, function components receive `ref` as a regular prop, so new components do not need `forwardRef`. Keep `forwardRef` only if the project still supports React 18.
- Split a component when it has more than one reason to change or when logic can be reused as a custom hook; do not split on line count alone.
## Hooks
- Call hooks only at the top level of components and custom hooks; name custom hooks `useSomething`. Enable `eslint-plugin-react-hooks`.
- Treat `useMemo`, `useCallback` and `React.memo` as performance tools: add them when a measurement or a memoized child needs a stable reference. If the project uses React Compiler, do not add them by default.
- Do not use `useEffect` to derive state from props or other state; compute it during render.
## State
- `useState` for simple local state, `useReducer` when updates depend on several fields or follow named transitions.
- Server data belongs in the project's data layer (Server Components or loaders, TanStack Query, SWR), not in global client state.
- Use a global client store (Zustand, Jotai, context) only for client state that many distant components share. If props pass through components that do not use them, try composition (`children`) before a store.
## File organization
Follow the existing structure. When none exists, group by feature:
```
src/
├── components/ui/ # shared primitives (Button, Input)
├── features/<name>/ # feature components, hooks and tests together
├── hooks/ # hooks shared across features
├── lib/ # framework-agnostic utilities
└── types/ # shared types
```
## Error boundaries
- Wrap independent sections (a widget, a route segment) in error boundaries with a useful fallback and a retry. Error boundaries are class components in React; use the `react-error-boundary` package or the framework's error file (`error.tsx` in Next.js) instead of writing one per feature.
- Report caught errors to the project's monitoring service.
## Performance
- Profile with React DevTools before optimizing.
- Virtualize long lists, lazy-load heavy routes and components with `lazy` and `Suspense`, and keep large dependencies out of the initial bundle.
Cursor rules for Next.js 14+ App Router projects updated for Next.js 16 with async params, validated Route Handlers, generateMetadata and proxy.ts in place of middleware.
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.
Cursor
frontend
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
Creating a reusable Button for a React 19 + TypeScript app; the old component re-declares onClick/disabled props, wraps every handler in useCallback and uses forwardRef.
Use the React TypeScript Best Practices rule to rewrite src/components/ui/Button.tsx so it accepts all native button props and a ref, with primary/secondary variants.
A function component whose props extend ComponentProps<'button'>, defaults type to 'button', forwards remaining props (including ref in React 19) to the <button>, and no forwardRef or speculative useCallback; the answer notes forwardRef is still needed if React 18 must be supported.
Cursor applies project rules stored as .mdc files in .cursor/rules. Each rule's frontmatter (description, globs, alwaysApply) decides whether it is always included, attached to matching files, chosen by the agent from its description, or only used when you @-mention it. Cursor also reads AGENTS.md in the project root and subdirectories for plain-Markdown instructions.
.cursor/rules/react-typescript-best-practices.mdcStarting point, not a tested configuration for your repository. Attached when files matching the globs are in context (alwaysApply: false with globs). Review the instructions, adapt the file paths and run your project's existing checks.
---
description: "React TypeScript Best Practices project conventions"
globs: "**/*"
alwaysApply: false
---
Apply these rules when writing React components in TypeScript.
## Components
- Write function components with typed props. Use `interface` or `type` consistently with the codebase; a `type` is needed for unions and intersections.
- Follow the project's export convention. Named exports are easier to find and rename; frameworks that require a default export (for example Next.js `page.tsx` and `layout.tsx`) are the exception.
- Extend native element props instead of re-declaring them, so `disabled`, `aria-*` and event handlers pass through:
```tsx
import type { ComponentProps } from 'react';
type ButtonProps = ComponentProps<'button'> & {
variant?: 'primary' | 'secondary';
};
export function Button({ variant = 'primary', type = 'button', className, ...props }: ButtonProps) {
return (
<button
type={type}
className={['btn', `btn-${variant}`, className].filter(Boolean).join(' ')}
{...props}
/>
);
}
```
- In React 19, function components receive `ref` as a regular prop, so new components do not need `forwardRef`. Keep `forwardRef` only if the project still supports React 18.
- Split a component when it has more than one reason to change or when logic can be reused as a custom hook; do not split on line count alone.
## Hooks
- Call hooks only at the top level of components and custom hooks; name custom hooks `useSomething`. Enable `eslint-plugin-react-hooks`.
- Treat `useMemo`, `useCallback` and `React.memo` as performance tools: add them when a measurement or a memoized child needs a stable reference. If the project uses React Compiler, do not add them by default.
- Do not use `useEffect` to derive state from props or other state; compute it during render.
## State
- `useState` for simple local state, `useReducer` when updates depend on several fields or follow named transitions.
- Server data belongs in the project's data layer (Server Components or loaders, TanStack Query, SWR), not in global client state.
- Use a global client store (Zustand, Jotai, context) only for client state that many distant components share. If props pass through components that do not use them, try composition (`children`) before a store.
## File organization
Follow the existing structure. When none exists, group by feature:
```
src/
├── components/ui/ # shared primitives (Button, Input)
├── features/<name>/ # feature components, hooks and tests together
├── hooks/ # hooks shared across features
├── lib/ # framework-agnostic utilities
└── types/ # shared types
```
## Error boundaries
- Wrap independent sections (a widget, a route segment) in error boundaries with a useful fallback and a retry. Error boundaries are class components in React; use the `react-error-boundary` package or the framework's error file (`error.tsx` in Next.js) instead of writing one per feature.
- Report caught errors to the project's monitoring service.
## Performance
- Profile with React DevTools before optimizing.
- Virtualize long lists, lazy-load heavy routes and components with `lazy` and `Suspense`, and keep large dependencies out of the initial bundle.
Ask for a small change to one matching file. Check the tool's loaded context or instruction view, inspect the diff for the requested convention, and run the relevant build or test. A well-formatted file does not guarantee the model follows every instruction.
Cursor's docs recommend keeping individual rules under 500 lines and splitting large rules into composable ones. Cursor's documentation states that it supports AGENTS.md in the project root and in subdirectories. Use AGENTS.md for plain-Markdown instructions you also want other tools to read, and .mdc rules when you need Cursor's scoping options (globs, description-based selection, always-apply).
Documentation checked 2026-11-05: Cursor docs: Rules
Cursor rules for Next.js 14+ App Router projects updated for Next.js 16 with async params, validated Route Handlers, generateMetadata and proxy.ts in place of middleware.
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.