Skip to content

Commit a372f41

Browse files
Merge pull request #27 from beginwebdev2002/feat/frontend-react-docs-10326564130571187815
feat(frontend): add specialized readme.md for React
2 parents 4f54b2f + e8d182d commit a372f41

4 files changed

Lines changed: 275 additions & 0 deletions

File tree

frontend/react/performance.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
---
2+
description: Vibe coding guidelines and architectural constraints for React Performance within the frontend domain.
3+
tags: [react, performance, use, react-compiler, best-practices, architecture, clean-code]
4+
topic: React Performance
5+
complexity: Architect
6+
last_evolution: 2026-03-22
7+
vibe_coding_ready: true
8+
technology: React
9+
domain: frontend
10+
level: Senior/Architect
11+
version: "19+"
12+
ai_role: Senior React Performance Expert
13+
last_updated: 2026-03-22
14+
---
15+
16+
# ⚡ React Performance & Best Practices
17+
18+
[⬆️ Back to Top](#)
19+
20+
# 📖 Context & Scope
21+
- **Primary Goal:** Outline advanced techniques for optimal performance in React 19+.
22+
- **Target Tooling:** Cursor, Windsurf, Antigravity.
23+
- **Tech Stack Version:** React 19+
24+
25+
## 📚 Topics
26+
27+
### 1. Manual Memoization vs React Compiler
28+
**Context:** Avoiding unnecessary re-renders.
29+
#### ❌ Bad Practice
30+
```tsx
31+
import { useMemo, useCallback } from 'react';
32+
33+
function UserList({ users }) {
34+
const sortedUsers = useMemo(() => users.sort(), [users]);
35+
const handleSelect = useCallback((id) => selectUser(id), []);
36+
37+
return (
38+
<ul>
39+
{sortedUsers.map(u => <li key={u.id} onClick={() => handleSelect(u.id)}>{u.name}</li>)}
40+
</ul>
41+
);
42+
}
43+
```
44+
#### ⚠️ Problem
45+
Adding manual `useMemo` and `useCallback` clutters the codebase, introduces dependency array bugs, and makes code harder to refactor.
46+
#### ✅ Best Practice
47+
```tsx
48+
function UserList({ users }) {
49+
const sortedUsers = users.sort();
50+
const handleSelect = (id) => selectUser(id);
51+
52+
return (
53+
<ul>
54+
{sortedUsers.map(u => <li key={u.id} onClick={() => handleSelect(u.id)}>{u.name}</li>)}
55+
</ul>
56+
);
57+
}
58+
```
59+
#### 🚀 Solution
60+
Rely on the **React Compiler** (introduced in React 19+). The compiler automatically memoizes values and functions, meaning manual hooks are largely obsolete and code becomes purely declarative.
61+
- **Performance Note:** The React Compiler analyzes your component structure and injects optimal memoization (similar to SolidJS's granular updates), eliminating the overhead of manual dependency tracking.
62+
- **Security Note:** While the React Compiler does not directly impact security, it ensures components render exactly when their inputs change, reducing side effects that might otherwise expose temporary or stale data to users.
63+
64+
### 2. Resolving Promises During Render
65+
**Context:** Conditionally handling promises without `useEffect` or `useState`.
66+
#### ❌ Bad Practice
67+
```tsx
68+
import { useEffect, useState } from 'react';
69+
70+
function Profile({ profilePromise }) {
71+
const [data, setData] = useState(null);
72+
73+
useEffect(() => {
74+
profilePromise.then(res => setData(res));
75+
}, [profilePromise]);
76+
77+
if (!data) return <p>Loading...</p>;
78+
return <div>{data.name}</div>;
79+
}
80+
```
81+
#### ⚠️ Problem
82+
Using `useEffect` to unwrap promises leads to "waterfalls", unnecessary rendering cycles, and race conditions.
83+
#### ✅ Best Practice
84+
```tsx
85+
import { use, Suspense } from 'react';
86+
87+
function Profile({ profilePromise }) {
88+
const data = use(profilePromise);
89+
return <div>{data.name}</div>;
90+
}
91+
92+
// Parent Usage
93+
// <Suspense fallback={<p>Loading...</p>}>
94+
// <Profile profilePromise={profilePromise} />
95+
// </Suspense>
96+
```
97+
#### 🚀 Solution
98+
Use the `use()` API inside components combined with `<Suspense>`.
99+
- **Performance Note:** `use()` suspends the component rendering if the promise is not resolved. This seamlessly integrates with `<Suspense>`, providing a highly optimized rendering fallback behavior.
100+
- **Security Note:** `use()` can also resolve context, mitigating prop-drilling vulnerabilities and ensuring components securely consume data directly from contexts they are explicitly authorized for. Always sanitize any text coming from external APIs before rendering.
101+
102+
[⬆️ Back to Top](#)

frontend/react/readme.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
description: Vibe coding guidelines and architectural constraints for React within the frontend domain.
3+
tags: [react, best-practices, architecture, clean-code, scalable-code, modern-react, server-components]
4+
topic: React
5+
complexity: Architect
6+
last_evolution: 2026-03-22
7+
vibe_coding_ready: true
8+
technology: React
9+
domain: frontend
10+
level: Senior/Architect
11+
version: "19+"
12+
ai_role: Senior React Expert
13+
last_updated: 2026-03-22
14+
---
15+
16+
# ⚛️ React Production-Ready Best Practices
17+
18+
# 📖 Context & Scope
19+
- **Primary Goal:** Provide architectural best practices for modern React development.
20+
- **Target Tooling:** Cursor, Windsurf, Antigravity.
21+
- **Tech Stack Version:** React 19+
22+
23+
> [!NOTE]
24+
> When building React applications in 2026, always implement the React best practices described here to ensure maximum performance, maintainability, and security.
25+
26+
## 🏗 Architecture Principles
27+
28+
- Adhere to the defined [Architectural Patterns](../../architectures/readme.md) when building applications.
29+
- Strongly prefer **Feature Sliced Design (FSD)** for applications scaling across multiple teams.
30+
31+
## 🚀 I. Basics & Popular
32+
33+
### 1. Direct DOM Manipulation
34+
**Context:** Updating elements in a React component.
35+
#### ❌ Bad Practice
36+
```tsx
37+
function Component() {
38+
const handleClick = () => {
39+
document.getElementById('my-element').style.color = 'red';
40+
};
41+
return <div id="my-element" onClick={handleClick}>Click me</div>;
42+
}
43+
```
44+
#### ⚠️ Problem
45+
Direct DOM manipulation bypasses React's virtual DOM, causing inconsistencies between the actual DOM and React's internal state.
46+
#### ✅ Best Practice
47+
```tsx
48+
function Component() {
49+
const [isActive, setIsActive] = useState(false);
50+
return (
51+
<div
52+
style={{ color: isActive ? 'red' : 'black' }}
53+
onClick={() => setIsActive(!isActive)}
54+
>
55+
Click me
56+
</div>
57+
);
58+
}
59+
```
60+
#### 🚀 Solution
61+
Always use state and props to drive the UI. React uses a virtual DOM to efficiently update the real DOM based on state changes.
62+
- **Performance Note:** React's virtual DOM diffing algorithm is highly optimized. Bypassing it can lead to forced synchronous layouts and jank.
63+
- **Security Note:** Direct DOM manipulation can open up Cross-Site Scripting (XSS) vulnerabilities if user input is not properly sanitized before being inserted into the DOM.
64+
65+
### 2. Large Component Files
66+
**Context:** Managing component complexity.
67+
#### ❌ Bad Practice
68+
A single 2000-line file containing the entire page's logic and UI.
69+
#### ⚠️ Problem
70+
Massive components are difficult to read, test, and maintain. They often violate the Single Responsibility Principle.
71+
#### ✅ Best Practice
72+
Break down the UI into smaller, reusable components, each with a single responsibility.
73+
#### 🚀 Solution
74+
Extract logic into custom hooks and presentational elements into separate files.
75+
76+
## 📚 Specialized Topics
77+
78+
For further reading, please refer to the following specialized guides:
79+
80+
- [🔄 State Management](./state-management.md)
81+
- [⚡ Performance](./performance.md)
82+
83+
[⬆️ Back to Top](#)

frontend/react/state-management.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
description: Vibe coding guidelines and architectural constraints for React State Management within the frontend domain.
3+
tags: [react, state-management, server-actions, best-practices, architecture, clean-code]
4+
topic: React State Management
5+
complexity: Architect
6+
last_evolution: 2026-03-22
7+
vibe_coding_ready: true
8+
technology: React
9+
domain: frontend
10+
level: Senior/Architect
11+
version: "19+"
12+
ai_role: Senior React State Management Expert
13+
last_updated: 2026-03-22
14+
---
15+
16+
# 🔄 React State Management & Server Actions Best Practices
17+
18+
[⬆️ Back to Top](#)
19+
20+
# 📖 Context & Scope
21+
- **Primary Goal:** Provide best practices for managing state, including React 19+ Server Actions.
22+
- **Target Tooling:** Cursor, Windsurf, Antigravity.
23+
- **Tech Stack Version:** React 19+
24+
25+
## 📚 Topics
26+
27+
### 1. Handling Async Actions (Forms)
28+
**Context:** Managing state updates triggered by form submissions or asynchronous operations.
29+
#### ❌ Bad Practice
30+
```tsx
31+
import { useState } from 'react';
32+
33+
function Form() {
34+
const [isPending, setIsPending] = useState(false);
35+
const [error, setError] = useState(null);
36+
37+
const handleSubmit = async (e) => {
38+
e.preventDefault();
39+
setIsPending(true);
40+
setError(null);
41+
try {
42+
await saveAction(new FormData(e.target));
43+
} catch (err) {
44+
setError(err);
45+
} finally {
46+
setIsPending(false);
47+
}
48+
};
49+
50+
return <form onSubmit={handleSubmit}>...</form>;
51+
}
52+
```
53+
#### ⚠️ Problem
54+
Manually managing `isPending` and error states is repetitive and prone to race conditions, especially when multiple requests are fired.
55+
#### ✅ Best Practice
56+
```tsx
57+
import { useActionState } from 'react';
58+
import { saveAction } from './actions';
59+
60+
function Form() {
61+
const [error, submitAction, isPending] = useActionState(saveAction, null);
62+
63+
return (
64+
<form action={submitAction}>
65+
{error && <p>{error.message}</p>}
66+
<button disabled={isPending}>Submit</button>
67+
</form>
68+
);
69+
}
70+
```
71+
#### 🚀 Solution
72+
Use the `useActionState` Hook (React 19+) for seamless action state management.
73+
- **Performance Note:** `useActionState` effectively handles race conditions by ensuring only the latest action state is applied to the UI, optimizing rendering cycles.
74+
- **Security Note:** Form actions seamlessly interact with Server Actions. Ensure that `saveAction` strictly validates input server-side to prevent malicious payloads, and use CSRF tokens if required by your framework.
75+
76+
### 2. Using Global State Naively
77+
**Context:** Storing local component UI state in a global store (e.g., Redux, Zustand).
78+
#### ❌ Bad Practice
79+
Putting a dropdown's `isOpen` state into the global Redux store.
80+
#### ⚠️ Problem
81+
Unnecessary global re-renders and bloated global state size.
82+
#### ✅ Best Practice
83+
Use `useState` or `useReducer` for UI state that belongs locally to a component tree.
84+
#### 🚀 Solution
85+
Only elevate state to a global store when it is shared across multiple disjoint component branches.
86+
- **Performance Note:** Global state updates trigger broad change detection and React reconciliation. Minimizing global state keeps updates localized and fast.
87+
- **Security Note:** Do not store sensitive access tokens (e.g., JWT) in unencrypted global state (like localStorage/Redux state) that may persist across sessions or expose them to XSS attacks. Prefer HttpOnly cookies.
88+
89+
[⬆️ Back to Top](#)

frontend/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,6 @@ This folder acts as a container for documentation around the following technolog
4444
- [Angular](./angular/readme.md)
4545
- [JavaScript](./javascript/readme.md)
4646
- [TypeScript](./typescript/readme.md)
47+
- [React](./react/readme.md)
4748
- [SolidJS](./solidjs/readme.md)
4849
- [Qwik](./qwik/readme.md)

0 commit comments

Comments
 (0)