agentleFS
openstory-soopenstory-so/openstory/.cursor/rules/react-rules.mdc

openstory / rules

Cursor rule678 starsChanged 2 months agoMITView on GitHub

React best practices and conventions for UI code

We split this react-rules.mdc into the rules it gives an agent

32 rules. About 0 are common, 31 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈31

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Keep components small - less than 100 lines

    line 8
  2. Distinctive:

    Remove as much logic as possible from components and views

    line 10
  3. Distinctive:

    Avoid using useEffect to fetch data or initialise state

    line 16
  4. Very specific:

    Use hooks, tanstack query, or the new use feature in react 19

    line 17
  5. Distinctive:

    use useEffect to update state when something else changes

    line 18
  6. Distinctive:

    If you have more than 3 useStates you might as well use reducers

    line 24
  7. Distinctive:

    Start with reducers which are passed through props

    line 35
  8. Distinctive:

    If that's too complicated, use React context

    line 36
  9. Distinctive:

    Use them to update state based on other state.

    line 44
  10. Distinctive:

    Avoid passing styles to components, or styling your components in views

    line 49
  11. Distinctive:

    Create components that are pre-styled

    line 50
  12. Distinctive:

    Create variants - e.g. small, medium, large - rather than size={18}

    line 51
  13. Distinctive:

    Avoid using styles in views as much as possible

    line 52
  14. Distinctive:

    Create constants for your theme or use a consistent structure

    line 56
  15. Distinctive:

    Use the theme fonts and colors

    line 57
  16. Distinctive:

    Avoid naming fonts and inluding color values directly in views and components

    line 58
  17. Distinctive:

    Use flexbox for every component

    line 62
  18. Distinctive:

    Don't pre-add padding or margin to the outside of components, unless there is a specific reason to

    line 66
  19. Distinctive:

    Use flexbox gap instead

    line 67
  20. Distinctive:

    Use PascalCase for component names, but kebab case for file-names

    line 71
  21. Distinctive:

    If using a 3rd party component library, wrap those components then include your wrapped components.

    line 76
  22. Distinctive:

    Don't create duplicates of components for minor variations.

    line 79
  23. Distinctive:

    Put your components into a high level components folder

    line 81
  24. Distinctive:

    Use an aliases or subpath imports "~" to import components from views.

    line 82
  25. Distinctive:

    Use flexbox and create rules for screen sizes

    line 86
  26. Distinctive:

    Avoid code that conditionally shows a complex component - this creates janky ui

    line 90
  27. Distinctive:

    Instead use the display css property to hide or show - this will precalculate everything in the component but not render it

    line 91
  28. Distinctive:

    Ensure the rules of hooks linting rule is on

    line 95
  29. Distinctive:

    Check out 0xlint and Biome

    line 96
  30. Distinctive:

    All views should be routable - meaning you can get to them via a route

    line 100
  31. Distinctive:

    No view should rely on variables or parameters from another

    line 101
  32. Distinctive:

    Avoid barrelled imports as much as possible _unless_ you are planning to package that library for others

    line 109

≈ “Common” is an estimate: at least 1 in 5 of the 6,350 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
react-rules.mdc
---
description: React best practices and conventions for UI code
globs: *.tsx
alwaysApply: false
---
## 1. Use as little React as possible

- Keep components small - less than 100 lines
- Views reference components
- Remove as much logic as possible from components and views
- Externalize functions rather and keep external functions _vanilla typescript_
- Vanilla typescript is easier to test, and package for other uses

## 2. Avoid using useEffect

- Avoid using useEffect to fetch data or initialise state
- Use hooks, tanstack query, or the new `use` feature in react 19
- use useEffect to update state when something else changes

## 3. Use useState sparingly

- Using a local variable is often more optimal - even if something has to be calculated
- useState uses reducers under the hood
- If you have more than 3 useStates you might as well use reducers

## 4. Use React.FC and expand props

- Expand props so that each parameter is named
- It's easier to know which props are not used

## 5. Avoid globals and global state

- Globals including auth globals, often lead to race conditions
- Global state is only useful in rare cases. SPAs with signifant complexity
- Start with reducers which are passed through props
- If that's too complicated, use React context
- As a last resort for very complicated SPAs - use zustand

## 6. Use reducers

- Reducers are great - read up on them and understand them
- They keep all state update logic in one place
- They are vanilla typescript - keep them that way
- Use them to update state based on other state.
- Note that updating any part of the state returned from a reducer will cause a re-render if the whole state is a parameter - you can pass just parts of it

## 7. Avoid passing style to components

- Avoid passing styles to components, or styling your components in views
- Create components that are pre-styled
- Create variants - e.g. small, medium, large - rather than size={18}
- Avoid using styles in views as much as possible

## 8. Use the theme

- Create constants for your theme or use a consistent structure
- Use the theme fonts and colors
- Avoid naming fonts and inluding color values directly in views and components

## 9. Use flexbox

- Use flexbox for every component

## 10. Avoid margin

- Don't pre-add padding or margin to the outside of components, unless there is a specific reason to
- Use flexbox gap instead

## 11. Use kebab-case for file names

- Use PascalCase for component names, but kebab case for file-names
- PascalCase can often give you issues in git as case sensitive file names is not supported on all platforms

## 12. Create a component library

- If using a 3rd party component library, wrap those components then include your wrapped components. It makes it easier to change libraries
- Shadcn makes this easy - it includes the source in your repo, and imports only primitives from that source.
- You don't need to wrap Shadcn generated components - just edit the component source with your changes
- Don't create duplicates of components for minor variations. Create a variation
- Customise the component if there's a new variation - don't style from the outside
- Put your components into a high level components folder
- Use an aliases or subpath imports "~" to import components from views.

## 13. Avoid hard coding width or height

- Use flexbox and create rules for screen sizes

## 14. Use a show prop if you need to hide something

- Avoid code that conditionally shows a complex component - this creates janky ui
- Instead use the display css property to hide or show - this will precalculate everything in the component but not render it

## 15. Use eslint or equivalent

- Ensure the rules of hooks linting rule is on
- Check out 0xlint and Biome

## 16. Views are routes

- All views should be routable - meaning you can get to them via a route
- No view should rely on variables or parameters from another
- A view can be accessed in any order
- Pass params on the url. You can use url segments for ids, search params should be optional
- Name views with the same name as the route - or place in a folder with that name

## 17. Avoid default exports

- It's more efficient to export the component directly than to import a default
- Avoid barrelled imports as much as possible _unless_ you are planning to package that library for others

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

Reviews can't be read right now.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.