nurosys-agents 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.agent/INSTRUCTIONS.md +82 -0
- package/.agent/README.md +483 -0
- package/.agent/backend/skills/architect/SKILL.md +436 -0
- package/.agent/backend/skills/auth-and-permissions/SKILL.md +168 -0
- package/.agent/backend/skills/brainstorm/SKILL.md +127 -0
- package/.agent/backend/skills/code-reviewer/SKILL.md +324 -0
- package/.agent/backend/skills/create-blueprint/SKILL.md +650 -0
- package/.agent/backend/skills/debug-issue/SKILL.md +53 -0
- package/.agent/backend/skills/explore-codebase/SKILL.md +45 -0
- package/.agent/backend/skills/quick-execute/SKILL.md +99 -0
- package/.agent/backend/skills/refactor-safely/SKILL.md +46 -0
- package/.agent/backend/skills/security-assessment/SKILL.md +174 -0
- package/.agent/backend/workflows/module-runner.claude.md +226 -0
- package/.agent/backend/workflows/module-runner.codex.md +155 -0
- package/.agent/backend/workflows/module-runner.cursor.md +212 -0
- package/.agent/frontend/skills/architect/SKILL.md +644 -0
- package/.agent/frontend/skills/auth-and-permissions/SKILL.md +43 -0
- package/.agent/frontend/skills/create-blueprint/SKILL.md +635 -0
- package/.agent/frontend/skills/debug-issue/SKILL.md +28 -0
- package/.agent/frontend/skills/explore-codebase/SKILL.md +29 -0
- package/.agent/frontend/skills/feature-workflow/SKILL.md +61 -0
- package/.agent/frontend/skills/react-quality-review/SKILL.md +126 -0
- package/.agent/frontend/skills/react-quality-review/examples.md +24 -0
- package/.agent/frontend/skills/react-quality-review/rules/_sections.md +26 -0
- package/.agent/frontend/skills/react-quality-review/rules/_template.md +28 -0
- package/.agent/frontend/skills/react-quality-review/rules/advanced-event-handler-refs.md +55 -0
- package/.agent/frontend/skills/react-quality-review/rules/advanced-init-once.md +42 -0
- package/.agent/frontend/skills/react-quality-review/rules/react-rules-calling.md +66 -0
- package/.agent/frontend/skills/react-quality-review/rules/react-rules-hooks.md +91 -0
- package/.agent/frontend/skills/react-quality-review/rules/react-rules-purity.md +121 -0
- package/.agent/frontend/skills/react-quality-review/rules/rendering-activity.md +26 -0
- package/.agent/frontend/skills/react-quality-review/rules/rendering-conditional-render.md +40 -0
- package/.agent/frontend/skills/react-quality-review/rules/rendering-content-visibility.md +38 -0
- package/.agent/frontend/skills/react-quality-review/rules/rendering-hoist-jsx.md +46 -0
- package/.agent/frontend/skills/react-quality-review/rules/rendering-usetransition-loading.md +75 -0
- package/.agent/frontend/skills/react-quality-review/rules/rerender-defer-reads.md +39 -0
- package/.agent/frontend/skills/react-quality-review/rules/rerender-dependencies.md +45 -0
- package/.agent/frontend/skills/react-quality-review/rules/rerender-derived-state-no-effect.md +40 -0
- package/.agent/frontend/skills/react-quality-review/rules/rerender-derived-state.md +29 -0
- package/.agent/frontend/skills/react-quality-review/rules/rerender-functional-setstate.md +74 -0
- package/.agent/frontend/skills/react-quality-review/rules/rerender-lazy-state-init.md +58 -0
- package/.agent/frontend/skills/react-quality-review/rules/rerender-memo-with-default-value.md +38 -0
- package/.agent/frontend/skills/react-quality-review/rules/rerender-memo.md +44 -0
- package/.agent/frontend/skills/react-quality-review/rules/rerender-move-effect-to-event.md +45 -0
- package/.agent/frontend/skills/react-quality-review/rules/rerender-no-inline-components.md +82 -0
- package/.agent/frontend/skills/react-quality-review/rules/rerender-simple-expression-in-memo.md +35 -0
- package/.agent/frontend/skills/react-quality-review/rules/rerender-transitions.md +40 -0
- package/.agent/frontend/skills/react-quality-review/rules/rerender-use-ref-transient-values.md +73 -0
- package/.agent/frontend/skills/refactor-safely/SKILL.md +29 -0
- package/.agent/frontend/skills/vuexy-component-guide/SKILL.md +369 -0
- package/.agent/frontend/skills/vuexy-component-guide/examples.md +662 -0
- package/.agent/frontend/skills/vuexy-component-guide/reference.md +1036 -0
- package/.agent/frontend/workflows/build-feature-react.workflow.md +82 -0
- package/.agent/frontend/workflows/feature-module-runner.md +101 -0
- package/.agent/monolith/skills/architect/SKILL.md +648 -0
- package/.agent/monolith/skills/auth-and-permissions/SKILL.md +43 -0
- package/.agent/monolith/skills/code-reviewer/SKILL.md +281 -0
- package/.agent/monolith/skills/create-blueprint/SKILL.md +635 -0
- package/.agent/monolith/skills/debug-issue/SKILL.md +28 -0
- package/.agent/monolith/skills/explore-codebase/SKILL.md +29 -0
- package/.agent/monolith/skills/feature-workflow/SKILL.md +61 -0
- package/.agent/monolith/skills/react-quality-review/SKILL.md +126 -0
- package/.agent/monolith/skills/react-quality-review/examples.md +24 -0
- package/.agent/monolith/skills/react-quality-review/rules/_sections.md +26 -0
- package/.agent/monolith/skills/react-quality-review/rules/_template.md +28 -0
- package/.agent/monolith/skills/react-quality-review/rules/advanced-event-handler-refs.md +55 -0
- package/.agent/monolith/skills/react-quality-review/rules/advanced-init-once.md +42 -0
- package/.agent/monolith/skills/react-quality-review/rules/react-rules-calling.md +66 -0
- package/.agent/monolith/skills/react-quality-review/rules/react-rules-hooks.md +91 -0
- package/.agent/monolith/skills/react-quality-review/rules/react-rules-purity.md +121 -0
- package/.agent/monolith/skills/react-quality-review/rules/rendering-activity.md +26 -0
- package/.agent/monolith/skills/react-quality-review/rules/rendering-conditional-render.md +40 -0
- package/.agent/monolith/skills/react-quality-review/rules/rendering-content-visibility.md +38 -0
- package/.agent/monolith/skills/react-quality-review/rules/rendering-hoist-jsx.md +46 -0
- package/.agent/monolith/skills/react-quality-review/rules/rendering-usetransition-loading.md +75 -0
- package/.agent/monolith/skills/react-quality-review/rules/rerender-defer-reads.md +39 -0
- package/.agent/monolith/skills/react-quality-review/rules/rerender-dependencies.md +45 -0
- package/.agent/monolith/skills/react-quality-review/rules/rerender-derived-state-no-effect.md +40 -0
- package/.agent/monolith/skills/react-quality-review/rules/rerender-derived-state.md +29 -0
- package/.agent/monolith/skills/react-quality-review/rules/rerender-functional-setstate.md +74 -0
- package/.agent/monolith/skills/react-quality-review/rules/rerender-lazy-state-init.md +58 -0
- package/.agent/monolith/skills/react-quality-review/rules/rerender-memo-with-default-value.md +38 -0
- package/.agent/monolith/skills/react-quality-review/rules/rerender-memo.md +44 -0
- package/.agent/monolith/skills/react-quality-review/rules/rerender-move-effect-to-event.md +45 -0
- package/.agent/monolith/skills/react-quality-review/rules/rerender-no-inline-components.md +82 -0
- package/.agent/monolith/skills/react-quality-review/rules/rerender-simple-expression-in-memo.md +35 -0
- package/.agent/monolith/skills/react-quality-review/rules/rerender-transitions.md +40 -0
- package/.agent/monolith/skills/react-quality-review/rules/rerender-use-ref-transient-values.md +73 -0
- package/.agent/monolith/skills/refactor-safely/SKILL.md +29 -0
- package/.agent/monolith/skills/vuexy-component-guide/SKILL.md +369 -0
- package/.agent/monolith/skills/vuexy-component-guide/examples.md +662 -0
- package/.agent/monolith/skills/vuexy-component-guide/reference.md +1036 -0
- package/.agent/monolith/workflows/add-new-api-feature-module.md +63 -0
- package/.agent/monolith/workflows/backend-quality-review.md +27 -0
- package/.agent/monolith/workflows/build-feature-backend.workflow.md +91 -0
- package/.agent/monolith/workflows/build-feature-react.workflow.md +82 -0
- package/.agent/monolith/workflows/feature-module-runner.md +97 -0
- package/.agent/templates/FEATURE_PLAN.md +42 -0
- package/.agent/templates/MODULE.md +45 -0
- package/.agent/templates/REVIEW_REPORT.md +44 -0
- package/.agent/templates/SECURITY_REPORT.md +70 -0
- package/.agent/templates/TEST_PLAN.md +49 -0
- package/README.md +131 -0
- package/package.json +42 -0
- package/scripts/setup-rules.js +224 -0
- package/scripts/setup.js +518 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: feature-workflow
|
|
3
|
+
description: Enforce requirement clarification, study, detailed planning, implementation, quality review, and testing for every feature/refactor.
|
|
4
|
+
when_to_use:
|
|
5
|
+
- Any new feature in `src/app`, `src/components`, `src/services`, or `src/views`
|
|
6
|
+
- Any refactor that changes behavior
|
|
7
|
+
references:
|
|
8
|
+
- project-memory/constitution.md
|
|
9
|
+
- project-memory/repo-map.md
|
|
10
|
+
- project-memory/quality-playbook.md
|
|
11
|
+
- project-memory/core-memory.md
|
|
12
|
+
- .agent/skills/react-quality-review/SKILL.md
|
|
13
|
+
- .agent/templates/FEATURE_PLAN.md
|
|
14
|
+
- .agent/templates/REVIEW_REPORT.md
|
|
15
|
+
- .agent/templates/TEST_PLAN.md
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
# Feature Workflow Skill
|
|
19
|
+
|
|
20
|
+
## Required sequence (no skipping)
|
|
21
|
+
|
|
22
|
+
0. **Foundation read (mandatory)**
|
|
23
|
+
- Read `project-memory/constitution.md`, `project-memory/repo-map.md`, `project-memory/auth-model.md`, and `project-memory/core-memory.md`.
|
|
24
|
+
1. **Requirement clarification**
|
|
25
|
+
- Ask clarifying questions where needed.
|
|
26
|
+
- Capture hard constraints (auth, permissions, performance, data contracts, deadlines).
|
|
27
|
+
2. **Codebase study**
|
|
28
|
+
- List relevant existing files/modules found before planning implementation.
|
|
29
|
+
3. **Detailed plan**
|
|
30
|
+
- Define new/changed functions and hooks.
|
|
31
|
+
- Define shared derivations/selectors and where computed values should live.
|
|
32
|
+
- List all files to be touched.
|
|
33
|
+
4. **Implementation**
|
|
34
|
+
- Execute the plan in dependency order.
|
|
35
|
+
5. **Quality review + refactor**
|
|
36
|
+
- Run `.agent/skills/react-quality-review/SKILL.md`.
|
|
37
|
+
- Produce `Documentation/reports/REVIEW_REPORT.md`.
|
|
38
|
+
- Apply required refactors from findings.
|
|
39
|
+
6. **Testing**
|
|
40
|
+
- Add/run unit tests for pure transforms/selectors.
|
|
41
|
+
- Add/run integration tests for pages/components where applicable.
|
|
42
|
+
7. **Update memory**
|
|
43
|
+
- Append the completed module/feature summary to `project-memory/core-memory.md`.
|
|
44
|
+
- Record key decisions, files changed, tests run, and any carry-forward notes.
|
|
45
|
+
|
|
46
|
+
## Mandatory artifacts
|
|
47
|
+
|
|
48
|
+
- Feature plan: `Documentation/features/<feature_name>/FEATURE_PLAN.md` or `Documentation/plans/FEATURE_PLAN.md` (from `.agent/templates/FEATURE_PLAN.md`)
|
|
49
|
+
- Review report: `Documentation/reports/REVIEW_REPORT.md` (from `.agent/templates/REVIEW_REPORT.md`, filled after implementation)
|
|
50
|
+
- Test plan: `Documentation/features/<feature_name>/TEST_PLAN.md` or `Documentation/plans/TEST_PLAN.md` (from `.agent/templates/TEST_PLAN.md`, filled and executed)
|
|
51
|
+
- `project-memory/core-memory.md` (updated after implementation)
|
|
52
|
+
|
|
53
|
+
## PR gate checklist
|
|
54
|
+
|
|
55
|
+
- [ ] Clarification questions + constraints documented.
|
|
56
|
+
- [ ] Codebase study lists relevant files reviewed.
|
|
57
|
+
- [ ] Detailed plan lists functions/hooks, shared derivations, computed-value ownership, file touch list.
|
|
58
|
+
- [ ] React quality review executed, `Documentation/reports/REVIEW_REPORT.md` produced, and refactors applied.
|
|
59
|
+
- [ ] Unit tests added for pure transforms/selectors.
|
|
60
|
+
- [ ] Integration tests added/updated for pages/components where applicable.
|
|
61
|
+
- [ ] `project-memory/core-memory.md` updated with implementation outcomes and next-step notes.
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: react-quality-review
|
|
3
|
+
description: >
|
|
4
|
+
Post-implementation quality gate for React/Next.js. Covers Rules of React,
|
|
5
|
+
re-render optimization, rendering performance, and advanced patterns (23 rules in 4
|
|
6
|
+
categories). Use after features/refactors and when reviewing React PRs.
|
|
7
|
+
when_to_use:
|
|
8
|
+
- After any feature implementation
|
|
9
|
+
- After any refactor (UI, hooks, services, routes)
|
|
10
|
+
- When writing or reviewing React components, hooks, or state management
|
|
11
|
+
- When optimizing re-render or rendering performance
|
|
12
|
+
references:
|
|
13
|
+
- project-memory/quality-playbook.md
|
|
14
|
+
- project-memory/repo-map.md
|
|
15
|
+
- project-memory/auth-model.md
|
|
16
|
+
- src/contexts/authContext.tsx
|
|
17
|
+
- src/utils/auth.ts
|
|
18
|
+
- src/services/
|
|
19
|
+
- .agent/skills/react-quality-review/rules/
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
# React Quality Review Skill
|
|
23
|
+
|
|
24
|
+
Run this skill immediately after coding, before merge. It combines the project's playbook checks with detailed React review rules (same taxonomy as the legacy `review-react` skill).
|
|
25
|
+
|
|
26
|
+
## References
|
|
27
|
+
|
|
28
|
+
- `project-memory/quality-playbook.md` — definitions and repo anchors
|
|
29
|
+
- `.agent/templates/REVIEW_REPORT.md` — report structure
|
|
30
|
+
- `.agent/skills/react-quality-review/rules/` — one file per rule (explanation, bad/good examples, links)
|
|
31
|
+
- `.agent/skills/react-quality-review/examples.md` — concrete examples
|
|
32
|
+
|
|
33
|
+
## Rule categories by priority
|
|
34
|
+
|
|
35
|
+
Performance and correctness are grouped into four categories (23 rules), ordered by typical impact:
|
|
36
|
+
|
|
37
|
+
| Priority | Category | Impact | Prefix | Rules |
|
|
38
|
+
|----------|----------|--------|--------|-------|
|
|
39
|
+
| 1 | Rules of React | CRITICAL | `react-rules-` | 3 |
|
|
40
|
+
| 2 | Re-render optimization | MEDIUM | `rerender-` | 13 |
|
|
41
|
+
| 3 | Rendering performance | MEDIUM | `rendering-` | 5 |
|
|
42
|
+
| 4 | Advanced patterns | LOW | `advanced-` | 2 |
|
|
43
|
+
|
|
44
|
+
### 1. Rules of React (CRITICAL)
|
|
45
|
+
|
|
46
|
+
- `react-rules-purity` — Components and hooks must be pure; no side effects during render
|
|
47
|
+
- `react-rules-hooks` — Only call hooks at the top level and from React functions
|
|
48
|
+
- `react-rules-calling` — Never call components as functions or pass hooks as values
|
|
49
|
+
|
|
50
|
+
### 2. Re-render optimization (MEDIUM)
|
|
51
|
+
|
|
52
|
+
- `rerender-no-inline-components` — Never define components inside other components
|
|
53
|
+
- `rerender-derived-state-no-effect` — Derive state during render, not in effects
|
|
54
|
+
- `rerender-memo` — Extract memoized child components to avoid re-renders
|
|
55
|
+
- `rerender-memo-with-default-value` — Hoist default non-primitive props outside memo
|
|
56
|
+
- `rerender-simple-expression-in-memo` — Don’t useMemo for simple primitive expressions
|
|
57
|
+
- `rerender-defer-reads` — Don’t subscribe to state only used in callbacks
|
|
58
|
+
- `rerender-dependencies` — Use primitive values in effect dependencies
|
|
59
|
+
- `rerender-derived-state` — Subscribe to derived booleans, not raw objects
|
|
60
|
+
- `rerender-functional-setstate` — Use functional setState for stable callbacks
|
|
61
|
+
- `rerender-lazy-state-init` — Pass an initializer to useState for expensive values
|
|
62
|
+
- `rerender-move-effect-to-event` — Move interaction logic from effects to event handlers
|
|
63
|
+
- `rerender-transitions` — Use startTransition for non-urgent state updates
|
|
64
|
+
- `rerender-use-ref-transient-values` — Use refs for frequently changing transient values
|
|
65
|
+
|
|
66
|
+
### 3. Rendering performance (MEDIUM)
|
|
67
|
+
|
|
68
|
+
- `rendering-hoist-jsx` — Hoist static JSX outside component functions
|
|
69
|
+
- `rendering-conditional-render` — Prefer ternary over `&&` for conditional rendering
|
|
70
|
+
- `rendering-usetransition-loading` — Prefer useTransition over manual loading state
|
|
71
|
+
- `rendering-content-visibility` — Use CSS `content-visibility: auto` for long lists
|
|
72
|
+
- `rendering-activity` — Use Activity for preserving hidden UI state
|
|
73
|
+
|
|
74
|
+
### 4. Advanced patterns (LOW)
|
|
75
|
+
|
|
76
|
+
- `advanced-event-handler-refs` — Store latest event handlers in refs for stable callbacks
|
|
77
|
+
- `advanced-init-once` — Initialize app-level singletons once, not per mount
|
|
78
|
+
|
|
79
|
+
## How to use the rule files
|
|
80
|
+
|
|
81
|
+
For any finding, open the matching file under `.agent/skills/react-quality-review/rules/`. Each file includes why it matters, incorrect vs correct examples, and reference links.
|
|
82
|
+
|
|
83
|
+
Examples:
|
|
84
|
+
|
|
85
|
+
- `.agent/skills/react-quality-review/rules/react-rules-purity.md`
|
|
86
|
+
- `.agent/skills/react-quality-review/rules/rerender-no-inline-components.md`
|
|
87
|
+
|
|
88
|
+
See `rules/_template.md` and `rules/_sections.md` for structure across rules.
|
|
89
|
+
|
|
90
|
+
## Required checks (cross-check with playbook)
|
|
91
|
+
|
|
92
|
+
Use `project-memory/quality-playbook.md` for definitions and repo anchors. Verify at least:
|
|
93
|
+
|
|
94
|
+
- Derived state and effects (`rerender-derived-state-no-effect`, `rerender-derived-state`, playbook)
|
|
95
|
+
- Effect misuse / event vs effect boundaries (`rerender-move-effect-to-event`, playbook)
|
|
96
|
+
- Memoization boundaries (`rerender-memo`, `rerender-simple-expression-in-memo`, playbook)
|
|
97
|
+
- Duplicate computations and repeated transforms (playbook + relevant `rerender-*` rules)
|
|
98
|
+
- Next.js App Router server/client boundaries (playbook)
|
|
99
|
+
- Dead code or duplicate logic residue (playbook)
|
|
100
|
+
|
|
101
|
+
## Steps
|
|
102
|
+
|
|
103
|
+
1. Read `project-memory/quality-playbook.md` and scan changed files against it and the rule list above; drill into specific `rules/*.md` files when something is unclear.
|
|
104
|
+
2. Write `Documentation/reports/REVIEW_REPORT.md` using `.agent/templates/REVIEW_REPORT.md`; include findings and exact file/line pointers. Map severe issues to `react-rules-*` where applicable.
|
|
105
|
+
3. For each finding, propose the smallest refactor that moves code toward playbook anchors and the relevant rule file.
|
|
106
|
+
4. Confirm auth boundaries stay centralized (`src/contexts/authContext.tsx`, `src/utils/auth.ts`, `src/configs/authConfig.ts`).
|
|
107
|
+
5. Attach the completed review report to PR notes.
|
|
108
|
+
|
|
109
|
+
## Review report template (fill this)
|
|
110
|
+
|
|
111
|
+
```md
|
|
112
|
+
## Findings
|
|
113
|
+
- [severity] <issue> — <file/path> — <playbook check and/or rule id, e.g. rerender-memo>
|
|
114
|
+
|
|
115
|
+
## Proposed refactors
|
|
116
|
+
- <refactor action> -> <target file(s)> -> <playbook anchor and/or rule file>
|
|
117
|
+
|
|
118
|
+
## Files to change
|
|
119
|
+
- <path>
|
|
120
|
+
- <path>
|
|
121
|
+
|
|
122
|
+
## Tests to add
|
|
123
|
+
- <test name> — <what regression it prevents> — <unit/integration/manual>
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
For concrete examples, see `.agent/skills/react-quality-review/examples.md`.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# React Quality Review Examples
|
|
2
|
+
|
|
3
|
+
Use these examples with `.agent/skills/react-quality-review/SKILL.md` and `project-memory/quality-playbook.md`.
|
|
4
|
+
|
|
5
|
+
## Example 1: Thin page composition (good)
|
|
6
|
+
|
|
7
|
+
- **File:** `src/app/[lang]/(dashboard)/(private)/apps/search-trend-research/page.tsx`
|
|
8
|
+
- **Pattern used well:** Page delegates most behavior to feature view modules instead of embedding heavy business logic.
|
|
9
|
+
- **Why this matters:** Route files stay easy to reason about and change risk is localized.
|
|
10
|
+
- **Preferred refactor if it degrades:** Move derived data and side effects to hooks/services and keep page as composition shell.
|
|
11
|
+
|
|
12
|
+
## Example 2: Dialog event flow (good target pattern)
|
|
13
|
+
|
|
14
|
+
- **File:** `src/components/dialogs/create-campaign/CampaignFormAccordion.tsx`
|
|
15
|
+
- **Pattern to preserve:** Keep submit behavior event-driven; avoid introducing effect-driven submit flags.
|
|
16
|
+
- **Why this matters:** Explicit event flow prevents accidental duplicate side effects.
|
|
17
|
+
- **Preferred refactor if it degrades:** Collapse boolean-triggered effects into explicit handler functions.
|
|
18
|
+
|
|
19
|
+
## Example 3: Shared transform extraction (poor -> refactor target)
|
|
20
|
+
|
|
21
|
+
- **File group:** `src/views/apps/prism-analytics/**` and `src/components/reports/utils/**`
|
|
22
|
+
- **Pattern used poorly:** Similar formatting/filtering logic can drift when duplicated across report variants.
|
|
23
|
+
- **Why this matters:** Inconsistent output and bug fixes repeated in multiple places.
|
|
24
|
+
- **Preferred refactor:** Extract canonical helpers into `src/utils/**` or focused service modules and reuse across views.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# Sections
|
|
2
|
+
|
|
3
|
+
This file defines all sections, their ordering, impact levels, and descriptions.
|
|
4
|
+
The section ID (in parentheses) is the filename prefix used to group rules.
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## 1. Rules of React (react-rules)
|
|
9
|
+
|
|
10
|
+
**Impact:** CRITICAL
|
|
11
|
+
**Description:** Fundamental rules from react.dev that ensure correctness. Components must be pure, Hooks must follow call rules, and components must not be called as functions.
|
|
12
|
+
|
|
13
|
+
## 2. Re-render Optimization (rerender)
|
|
14
|
+
|
|
15
|
+
**Impact:** MEDIUM
|
|
16
|
+
**Description:** Patterns to minimize unnecessary re-renders: proper memoization, derived state, functional setState, and effect dependency management.
|
|
17
|
+
|
|
18
|
+
## 3. Rendering Performance (rendering)
|
|
19
|
+
|
|
20
|
+
**Impact:** MEDIUM
|
|
21
|
+
**Description:** Techniques to optimize what and how React renders: hoisting static JSX, conditional rendering patterns, content-visibility, and transitions.
|
|
22
|
+
|
|
23
|
+
## 4. Advanced Patterns (advanced)
|
|
24
|
+
|
|
25
|
+
**Impact:** LOW
|
|
26
|
+
**Description:** Specialized techniques for edge cases: storing handlers in refs for stable callbacks and one-time initialization patterns.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Rule Title Here
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: Optional description of impact (e.g., "20-50% improvement")
|
|
5
|
+
tags: tag1, tag2
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Rule Title Here
|
|
9
|
+
|
|
10
|
+
**Impact: MEDIUM (optional impact description)**
|
|
11
|
+
|
|
12
|
+
Brief explanation of the rule and why it matters. This should be clear and concise, explaining the performance implications.
|
|
13
|
+
|
|
14
|
+
**Incorrect (description of what's wrong):**
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
// Bad code example here
|
|
18
|
+
const bad = example()
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
**Correct (description of what's right):**
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
// Good code example here
|
|
25
|
+
const good = example()
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Reference: [Link to documentation or resource](https://example.com)
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Store Event Handlers in Refs
|
|
3
|
+
impact: LOW
|
|
4
|
+
impactDescription: stable subscriptions
|
|
5
|
+
tags: advanced, hooks, refs, event-handlers, optimization
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Store Event Handlers in Refs
|
|
9
|
+
|
|
10
|
+
Store callbacks in refs when used in effects that shouldn't re-subscribe on callback changes.
|
|
11
|
+
|
|
12
|
+
**Incorrect (re-subscribes on every render):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
function useWindowEvent(event: string, handler: (e) => void) {
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
window.addEventListener(event, handler)
|
|
18
|
+
return () => window.removeEventListener(event, handler)
|
|
19
|
+
}, [event, handler])
|
|
20
|
+
}
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
**Correct (stable subscription):**
|
|
24
|
+
|
|
25
|
+
```tsx
|
|
26
|
+
function useWindowEvent(event: string, handler: (e) => void) {
|
|
27
|
+
const handlerRef = useRef(handler)
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
handlerRef.current = handler
|
|
30
|
+
}, [handler])
|
|
31
|
+
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
const listener = (e) => handlerRef.current(e)
|
|
34
|
+
window.addEventListener(event, listener)
|
|
35
|
+
return () => window.removeEventListener(event, listener)
|
|
36
|
+
}, [event])
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
**Alternative: use `useEffectEvent` if you're on latest React:**
|
|
41
|
+
|
|
42
|
+
```tsx
|
|
43
|
+
import { useEffectEvent } from 'react'
|
|
44
|
+
|
|
45
|
+
function useWindowEvent(event: string, handler: (e) => void) {
|
|
46
|
+
const onEvent = useEffectEvent(handler)
|
|
47
|
+
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
window.addEventListener(event, onEvent)
|
|
50
|
+
return () => window.removeEventListener(event, onEvent)
|
|
51
|
+
}, [event])
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
`useEffectEvent` provides a cleaner API for the same pattern: it creates a stable function reference that always calls the latest version of the handler.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Initialize App Once, Not Per Mount
|
|
3
|
+
impact: LOW-MEDIUM
|
|
4
|
+
impactDescription: avoids duplicate init in development
|
|
5
|
+
tags: initialization, useEffect, app-startup, side-effects
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Initialize App Once, Not Per Mount
|
|
9
|
+
|
|
10
|
+
Do not put app-wide initialization that must run once per app load inside `useEffect([])` of a component. Components can remount and effects will re-run. Use a module-level guard or top-level init in the entry module instead.
|
|
11
|
+
|
|
12
|
+
**Incorrect (runs twice in dev, re-runs on remount):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
function Comp() {
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
loadFromStorage()
|
|
18
|
+
checkAuthToken()
|
|
19
|
+
}, [])
|
|
20
|
+
|
|
21
|
+
// ...
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**Correct (once per app load):**
|
|
26
|
+
|
|
27
|
+
```tsx
|
|
28
|
+
let didInit = false
|
|
29
|
+
|
|
30
|
+
function Comp() {
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
if (didInit) return
|
|
33
|
+
didInit = true
|
|
34
|
+
loadFromStorage()
|
|
35
|
+
checkAuthToken()
|
|
36
|
+
}, [])
|
|
37
|
+
|
|
38
|
+
// ...
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Reference: [Initializing the application](https://react.dev/learn/you-might-not-need-an-effect#initializing-the-application)
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: React Calls Components and Hooks
|
|
3
|
+
impact: CRITICAL
|
|
4
|
+
impactDescription: breaks React's rendering model and optimization
|
|
5
|
+
tags: react-rules, components, hooks, calling-convention
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## React Calls Components and Hooks
|
|
9
|
+
|
|
10
|
+
**Impact: CRITICAL (breaks React's rendering model and optimization)**
|
|
11
|
+
|
|
12
|
+
React must control when components render and hooks execute. Calling them directly bypasses reconciliation, state management, and optimization.
|
|
13
|
+
|
|
14
|
+
### Rule 1: Never call component functions directly
|
|
15
|
+
|
|
16
|
+
**Incorrect (calling component as function):**
|
|
17
|
+
|
|
18
|
+
```tsx
|
|
19
|
+
function Parent() {
|
|
20
|
+
// This bypasses React's rendering, no proper lifecycle or state isolation
|
|
21
|
+
return <div>{Profile({ name: 'Alice' })}</div>
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**Correct (use JSX):**
|
|
26
|
+
|
|
27
|
+
```tsx
|
|
28
|
+
function Parent() {
|
|
29
|
+
return <div><Profile name="Alice" /></div>
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Calling a component as a function makes React treat it as part of the parent's render. This means:
|
|
34
|
+
- No separate fiber node for reconciliation
|
|
35
|
+
- State and effects are tied to the parent
|
|
36
|
+
- Keys and refs don't work as expected
|
|
37
|
+
|
|
38
|
+
### Rule 2: Never pass Hooks as regular values
|
|
39
|
+
|
|
40
|
+
**Incorrect (passing hook as prop):**
|
|
41
|
+
|
|
42
|
+
```tsx
|
|
43
|
+
function ChatRoom({ useStatus }) {
|
|
44
|
+
const status = useStatus() // Hook passed as value
|
|
45
|
+
return <p>{status}</p>
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
<ChatRoom useStatus={useOnlineStatus} />
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
**Correct (call hook directly, pass result as prop):**
|
|
52
|
+
|
|
53
|
+
```tsx
|
|
54
|
+
function ChatRoom({ status }) {
|
|
55
|
+
return <p>{status}</p>
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function ChatRoomWrapper() {
|
|
59
|
+
const status = useOnlineStatus()
|
|
60
|
+
return <ChatRoom status={status} />
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Passing hooks as values makes them opaque to React's static analysis, breaks the Rules of Hooks, and prevents the compiler from optimizing correctly.
|
|
65
|
+
|
|
66
|
+
Reference: [React calls Components and Hooks](https://react.dev/reference/rules/react-calls-components-and-hooks)
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Rules of Hooks
|
|
3
|
+
impact: CRITICAL
|
|
4
|
+
impactDescription: violating causes runtime errors and broken state
|
|
5
|
+
tags: react-rules, hooks, top-level
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Rules of Hooks
|
|
9
|
+
|
|
10
|
+
**Impact: CRITICAL (violating causes runtime errors and broken state)**
|
|
11
|
+
|
|
12
|
+
Hooks rely on a stable call order. Calling them conditionally or in loops breaks React's ability to track state correctly.
|
|
13
|
+
|
|
14
|
+
### Rule 1: Only call Hooks at the top level
|
|
15
|
+
|
|
16
|
+
**Incorrect (hook inside condition):**
|
|
17
|
+
|
|
18
|
+
```tsx
|
|
19
|
+
function Form({ showName }) {
|
|
20
|
+
if (showName) {
|
|
21
|
+
const [name, setName] = useState('') // Conditional hook call
|
|
22
|
+
}
|
|
23
|
+
const [email, setEmail] = useState('')
|
|
24
|
+
return <input value={email} onChange={e => setEmail(e.target.value)} />
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
**Correct (always call hooks, conditionally use values):**
|
|
29
|
+
|
|
30
|
+
```tsx
|
|
31
|
+
function Form({ showName }) {
|
|
32
|
+
const [name, setName] = useState('')
|
|
33
|
+
const [email, setEmail] = useState('')
|
|
34
|
+
return (
|
|
35
|
+
<>
|
|
36
|
+
{showName && <input value={name} onChange={e => setName(e.target.value)} />}
|
|
37
|
+
<input value={email} onChange={e => setEmail(e.target.value)} />
|
|
38
|
+
</>
|
|
39
|
+
)
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
**Incorrect (hook inside loop):**
|
|
44
|
+
|
|
45
|
+
```tsx
|
|
46
|
+
function Filters({ filters }) {
|
|
47
|
+
const values = []
|
|
48
|
+
for (const f of filters) {
|
|
49
|
+
values.push(useState(f.default)) // Hook in loop
|
|
50
|
+
}
|
|
51
|
+
return <>{/* ... */}</>
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
**Correct (extract to child component or use single state):**
|
|
56
|
+
|
|
57
|
+
```tsx
|
|
58
|
+
function Filters({ filters }) {
|
|
59
|
+
const [values, setValues] = useState(() =>
|
|
60
|
+
Object.fromEntries(filters.map(f => [f.id, f.default]))
|
|
61
|
+
)
|
|
62
|
+
return <>{/* ... */}</>
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Rule 2: Only call Hooks from React functions
|
|
67
|
+
|
|
68
|
+
**Incorrect (hook in regular function):**
|
|
69
|
+
|
|
70
|
+
```tsx
|
|
71
|
+
function getUser() {
|
|
72
|
+
const [user, setUser] = useState(null) // Not a React component
|
|
73
|
+
return user
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
**Correct (hook in component or custom hook):**
|
|
78
|
+
|
|
79
|
+
```tsx
|
|
80
|
+
function useUser() {
|
|
81
|
+
const [user, setUser] = useState(null)
|
|
82
|
+
return user
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function Profile() {
|
|
86
|
+
const user = useUser()
|
|
87
|
+
return <p>{user?.name}</p>
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Reference: [Rules of Hooks](https://react.dev/reference/rules/rules-of-hooks)
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Components and Hooks Must Be Pure
|
|
3
|
+
impact: CRITICAL
|
|
4
|
+
impactDescription: prevents bugs from non-deterministic rendering
|
|
5
|
+
tags: react-rules, purity, side-effects, immutability
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Components and Hooks Must Be Pure
|
|
9
|
+
|
|
10
|
+
**Impact: CRITICAL (prevents bugs from non-deterministic rendering)**
|
|
11
|
+
|
|
12
|
+
React assumes components are pure functions: same inputs, same output. Side effects during render cause bugs that are hard to reproduce, especially with concurrent features and Strict Mode.
|
|
13
|
+
|
|
14
|
+
### Rule 1: Components must be idempotent
|
|
15
|
+
|
|
16
|
+
**Incorrect (mutating external variable during render):**
|
|
17
|
+
|
|
18
|
+
```tsx
|
|
19
|
+
let count = 0
|
|
20
|
+
|
|
21
|
+
function Counter() {
|
|
22
|
+
count++ // Side effect during render
|
|
23
|
+
return <p>{count}</p>
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
**Correct (use state for mutable values):**
|
|
28
|
+
|
|
29
|
+
```tsx
|
|
30
|
+
function Counter() {
|
|
31
|
+
const [count, setCount] = useState(0)
|
|
32
|
+
return <p>{count}</p>
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Rule 2: Side effects must run outside of render
|
|
37
|
+
|
|
38
|
+
**Incorrect (side effect in render):**
|
|
39
|
+
|
|
40
|
+
```tsx
|
|
41
|
+
function ProductList({ items }) {
|
|
42
|
+
analytics.track('viewed', items.length) // Runs on every render
|
|
43
|
+
return <ul>{items.map(i => <li key={i.id}>{i.name}</li>)}</ul>
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
**Correct (side effect in effect or event handler):**
|
|
48
|
+
|
|
49
|
+
```tsx
|
|
50
|
+
function ProductList({ items }) {
|
|
51
|
+
useEffect(() => {
|
|
52
|
+
analytics.track('viewed', items.length)
|
|
53
|
+
}, [items.length])
|
|
54
|
+
return <ul>{items.map(i => <li key={i.id}>{i.name}</li>)}</ul>
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Rule 3: Props and state are immutable
|
|
59
|
+
|
|
60
|
+
**Incorrect (mutating props):**
|
|
61
|
+
|
|
62
|
+
```tsx
|
|
63
|
+
function List({ items }) {
|
|
64
|
+
items.push({ id: 'last' }) // Mutating prop
|
|
65
|
+
return <ul>{items.map(i => <li key={i.id}>{i.name}</li>)}</ul>
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
**Correct (create new values):**
|
|
70
|
+
|
|
71
|
+
```tsx
|
|
72
|
+
function List({ items }) {
|
|
73
|
+
const allItems = [...items, { id: 'last', name: 'Last' }]
|
|
74
|
+
return <ul>{allItems.map(i => <li key={i.id}>{i.name}</li>)}</ul>
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Rule 4: Return values and arguments to Hooks are immutable
|
|
79
|
+
|
|
80
|
+
**Incorrect (mutating hook return value):**
|
|
81
|
+
|
|
82
|
+
```tsx
|
|
83
|
+
function useItems() {
|
|
84
|
+
const items = useContext(ItemsContext)
|
|
85
|
+
items.sort() // Mutating context value
|
|
86
|
+
return items
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
**Correct (copy before mutating):**
|
|
91
|
+
|
|
92
|
+
```tsx
|
|
93
|
+
function useItems() {
|
|
94
|
+
const items = useContext(ItemsContext)
|
|
95
|
+
return [...items].sort()
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Rule 5: Values are immutable after being passed to JSX
|
|
100
|
+
|
|
101
|
+
**Incorrect (mutating after JSX use):**
|
|
102
|
+
|
|
103
|
+
```tsx
|
|
104
|
+
function Page({ title }) {
|
|
105
|
+
const config = { title }
|
|
106
|
+
const element = <Header config={config} />
|
|
107
|
+
config.title = 'changed' // Mutating after JSX use
|
|
108
|
+
return element
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
**Correct (don't mutate after passing to JSX):**
|
|
113
|
+
|
|
114
|
+
```tsx
|
|
115
|
+
function Page({ title }) {
|
|
116
|
+
const config = { title }
|
|
117
|
+
return <Header config={config} />
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Reference: [Components and Hooks must be pure](https://react.dev/reference/rules/components-and-hooks-must-be-pure)
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use Activity Component for Show/Hide
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: preserves state/DOM
|
|
5
|
+
tags: rendering, activity, visibility, state-preservation
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use Activity Component for Show/Hide
|
|
9
|
+
|
|
10
|
+
Use React's `<Activity>` to preserve state/DOM for expensive components that frequently toggle visibility.
|
|
11
|
+
|
|
12
|
+
**Usage:**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
import { Activity } from 'react'
|
|
16
|
+
|
|
17
|
+
function Dropdown({ isOpen }: Props) {
|
|
18
|
+
return (
|
|
19
|
+
<Activity mode={isOpen ? 'visible' : 'hidden'}>
|
|
20
|
+
<ExpensiveMenu />
|
|
21
|
+
</Activity>
|
|
22
|
+
)
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Avoids expensive re-renders and state loss.
|