304-react-patterns

A Chinese-language set of coding rules for React, Next.js, Redux, TypeScript, JavaScript, HTML, CSS, and related web tools. It covers planning, naming, formatting, type safety, and component design.

In plain words
What is it for?
Use it when planning or writing modern web interfaces, choosing names, structuring components, handling errors, and applying common JavaScript and TypeScript conventions.
Why use it?
It gives web projects consistent code style and makes the code easier to maintain and review.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/haidong-once/cursor-rules-collection/304-react-patterns
Clone the repo
git clone --depth 1 https://github.com/HaiDong-Once/cursor-rules-collection
Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 3,076 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.03076
Opus 5 $0.00000 $0.01538
Sonnet 5 $0.00000 $0.00615
Haiku 4.5 $0.00000 $0.00308

Measured 2d ago against content hash 3c964485f4e2, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

304-react-patterns scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

frontend/304-react-patterns.mdc · 385 lines

How it starts

The opening of the file, as written. The whole thing — 385 lines — stays where its author put it; the contents beside it link to each section on GitHub.

现代Web技术开发规范

本指南概述了使用现代Web技术(包括ReactJS、NextJS、Redux、TypeScript、JavaScript、HTML、CSS和UI框架)进行开发的最佳实践、约定和标准。

开发理念

  • 编写干净、可维护和可扩展的代码
  • 遵循SOLID原则
  • 函数式和声明式编程模式优于命令式
  • 强调类型安全和静态分析
  • 实践组件驱动开发

代码实现指南

规划阶段

  • 从逐步规划开始
  • 实现前编写详细的伪代码
  • 记录组件架构和数据流
  • 考虑边缘情况和错误场景

代码风格

  • 使用制表符进行缩进
  • 使用单引号表示字符串(除非需要避免转义)
  • 省略分号(除非需要消除歧义)
  • 消除未使用的变量
  • 在关键词后添加空格
  • 在函数声明括号前添加空格
  • 始终使用严格相等(===)而非宽松相等(==)
  • 中缀运算符两侧加空格
  • 逗号后添加空格
  • else语句与闭合花括号保持同一行
  • 多行if语句使用花括号
  • 回调中始终处理错误参数
  • 限制行长度在80个字符以内
  • 在多行对象/数组字面量中使用尾随逗号

命名约定

一般规则

  • 使用PascalCase(帕斯卡命名法)命名:
    • 组件
    • 类型定义
    • 接口
  • 使用kebab-case(短横线命名法)命名:
    • 目录名(例如:components/auth-wizard)
    • 文件名(例如:user-profile.tsx)
  • 使用camelCase(驼峰命名法)命名:
    • 变量
    • 函数
    • 方法
    • 钩子
    • 属性
    • Props
  • 使用UPPERCASE(大写)命名:
    • 环境变量
    • 常量
    • 全局配置

特定命名模式

  • 事件处理程序前缀为'handle':handleClick, handleSubmit
  • 布尔变量前缀使用动词:isLoading, hasError, canSubmit
  • 自定义钩子前缀为'use':useAuth, useForm
  • 使用完整词汇而非缩写,除了以下情况:
    • err(error的缩写)
    • req(request的缩写)
    • res(response的缩写)
    • props(properties的缩写)
    • ref(reference的缩写)
  • 数组命名:名词+List, 名词+s, 如:userList
  • 常量使用全大写下划线分隔命名,力求语义表达完整清楚:const API_ENDPOINT = "https://a"

命名严谨性(可读性)❤️

  • 代码中的命名严禁使用拼音与英文混合的方式,更不允许直接使用中文的方式(除特殊含义命名:如shuidi,jingdong)
  • 杜绝完全不规范的缩写,避免望文不知义

避免魔法数字

// 魔法数字
if (state === 1 || state === 2) {
  // ...
} else if (state === 3) {
  // ...
}

// 魔法数字改用常量映射
const UNPUBLISHED = 1;
const PUBLISHED = 2;
const DELETED = 3;

if (state === UNPUBLISHED || state === PUBLISHED) {
  // ...
} else if (state === DELETED) {
  // ...
}

React最佳实践

组件架构

  • 使用带TypeScript接口的函数组件
  • 使用function关键字定义组件
  • 将可复用逻辑提取到自定义钩子中
  • 实现合适的组件组合
  • 策略性地使用React.memo()提高性能
  • 在useEffect钩子中实现适当的清理
  • 保持组件职责单一,避免组件过大 ❤️
  • 函数式组件优先:React组件应优先使用函数组件,结合React Hooks管理状态 ❤️

JSX书写规范

  • JSX代码中每个标签属性必须换行书写 ❤️
<Component
  prop1="value1"
  prop2="value2"
/>
  • 在列表渲染中必须绑定key索引 ❤️
  • 避免在JSX中定义内联函数
  • 避免使用内联样式、内联事件,模板应简洁明了

React性能优化

  • 使用useCallback记忆回调函数
  • 为昂贵的计算实现useMemo
  • 使用动态导入实现代码分割
  • 在列表中正确实现key props(避免使用索引作为key)

Read the full file on GitHub · 385 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 385 lines · 0 tokens per session scan A 3c964485f4e2

Subscribe to this mod's changes

304-react-patterns is a cursor rule published in the GitHub repository HaiDong-Once/cursor-rules-collection (24 stars, last pushed 1y ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 3,076 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.