bulletproof-react-components
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseBulletproof React Components
健壮的React组件
Nine patterns that ensure React components survive real-world conditions beyond the happy path — SSR, hydration, concurrent rendering, portals, and more.
以下9种模式可确保React组件在常规场景之外的真实环境中稳定运行——包括SSR、hydration、并发渲染、Portals等场景。
How It Works
运作方式
- When writing or reviewing a reusable React component, consult the Quick Rules below
- For code examples and deeper explanation, read
./references/patterns.md - Run through the Checklist before shipping
- 编写或评审可复用React组件时,参考下方的快速规则
- 如需代码示例和深度解析,请阅读
./references/patterns.md - 发布前对照检查清单逐一验证
Quick Rules
快速规则
| # | Pattern | Rule |
|---|---|---|
| 1 | Server-Proof | Never call browser APIs ( |
| 2 | Hydration-Proof | Inject a synchronous inline |
| 3 | Instance-Proof | Use |
| 4 | Concurrent-Proof | Wrap server data-fetching in |
| 5 | Composition-Proof | Use Context instead of |
| 6 | Portal-Proof | Use |
| 7 | Transition-Proof | Wrap state updates in |
| 8 | Activity-Proof | Use |
| 9 | Future-Proof | Use |
| # | 模式 | 规则 |
|---|---|---|
| 1 | Server-Proof | 渲染阶段切勿调用浏览器API( |
| 2 | Hydration-Proof | 注入同步内联 |
| 3 | Instance-Proof | 所有生成的ID均使用 |
| 4 | Concurrent-Proof | 将服务端数据获取逻辑包裹在 |
| 5 | Composition-Proof | 使用Context而非 |
| 6 | Portal-Proof | 事件监听使用`ref.current?.ownerDocument.defaultView |
| 7 | Transition-Proof | 将状态更新包裹在 |
| 8 | Activity-Proof | 使用 |
| 9 | Future-Proof | 使用 |
Checklist
检查清单
When building a reusable React component, verify:
- No browser APIs called during render (server-proof)
- No hydration flash for client-storage values (hydration-proof)
- No hardcoded IDs; uses (instance-proof)
useId() - Server data fetches wrapped in (concurrent-proof)
cache() - Uses Context instead of (composition-proof)
cloneElement - Event listeners use (portal-proof)
ownerDocument.defaultView - State updates wrapped in where needed (transition-proof)
startTransition() - DOM side effects respect visibility (activity-proof)
<Activity> - Stable values use initializer, not
useState(future-proof)useMemo
构建可复用React组件时,请验证:
- 渲染阶段未调用浏览器API(Server-Proof)
- 客户端存储值未出现hydration闪烁(Hydration-Proof)
- 未硬编码ID;使用了(Instance-Proof)
useId() - 服务端数据获取已包裹在中(Concurrent-Proof)
cache() - 使用Context而非(Composition-Proof)
cloneElement - 事件监听使用(Portal-Proof)
ownerDocument.defaultView - 必要时将状态更新包裹在中(Transition-Proof)
startTransition() - DOM副作用遵循的可见性规则(Activity-Proof)
<Activity> - 稳定值使用初始化器,而非
useState(Future-Proof)useMemo
Present Results to User
向用户展示结果
When reviewing a component against these patterns, format as:
**Bulletproof Check: `<ComponentName>`**
| Pattern | Status | Notes |
|---------|--------|-------|
| Server-Proof | PASS/FAIL | ... |
| ... | ... | ... |
**Suggested fixes:**
1. ...评审组件是否符合这些模式时,请按以下格式输出:
**健壮性检查:`<ComponentName>`**
| 模式 | 状态 | 说明 |
|---------|--------|-------|
| Server-Proof | 通过/失败 | ... |
| ... | ... | ... |
**建议修复方案:**
1. ...References
参考资料
- — Detailed code examples (bad/good) for all nine patterns
./references/patterns.md
- — 包含所有9种模式的详细代码示例(错误/正确写法)
./references/patterns.md