Skip to content

Commit 0e2ff29

Browse files
committed
Add react-optimization.knowledge.md
1 parent 14b54cb commit 0e2ff29

File tree

1 file changed

+169
-0
lines changed

1 file changed

+169
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
# React Code Optimization Guidelines
2+
3+
When analyzing and modifying React code, examine the following patterns and implement the corresponding optimizations:
4+
5+
<Unnecessary Rerenders>
6+
7+
1. **Component-Level Rerendering:** When analyzing code, identify if components are rerendering unnecessarily. Look for and fix the following patterns:
8+
9+
- **State Changes High in the Tree:** Search for state changes high in the component tree that cause children to rerender unnecessarily. For example, if you see this pattern:
10+
11+
```jsx
12+
function ParentComponent() {
13+
const [count, setCount] = useState(0)
14+
return (
15+
<div>
16+
<ExpensiveChild unrelatedProp="something" />
17+
<button onClick={() => setCount(count + 1)}>Increment</button>
18+
</div>
19+
)
20+
}
21+
```
22+
23+
Modify the code to isolate state updates through component splitting or lifting state.
24+
25+
- **Lack of Memoization:** When child components rerender even though their props haven't changed, implement `React.memo`. For example, transform this:
26+
27+
```jsx
28+
const ChildComponent = ({ style }) => {
29+
console.log("ChildComponent rendering")
30+
return <div style={style}>Child</div>
31+
}
32+
```
33+
34+
Into this:
35+
36+
```jsx
37+
const ChildComponent = React.memo(({ style }) => {
38+
console.log("ChildComponent rendering")
39+
return <div style={style}>Child</div>
40+
})
41+
```
42+
43+
2. **Prop Instability:**
44+
45+
- **Inline Objects/Arrays:** Look for and fix object or array literals passed as props inline. For example, transform this problematic pattern:
46+
47+
```jsx
48+
function ParentComponent() {
49+
const [count, setCount] = useState(0)
50+
return (
51+
<div>
52+
<ChildComponent style={{ color: "blue" }} />
53+
<button onClick={() => setCount(count + 1)}>Increment</button>
54+
</div>
55+
)
56+
}
57+
```
58+
59+
Into either this:
60+
61+
```jsx
62+
const childStyle = { color: "blue" }
63+
64+
function ParentComponent() {
65+
const [count, setCount] = useState(0)
66+
return (
67+
<div>
68+
<ChildComponent style={childStyle} />
69+
<button onClick={() => setCount(count + 1)}>Increment</button>
70+
</div>
71+
)
72+
}
73+
```
74+
75+
Or using `useMemo`:
76+
77+
```jsx
78+
function ParentComponent() {
79+
const [count, setCount] = useState(0)
80+
const childStyle = useMemo(() => ({ color: "blue" }), [])
81+
return (
82+
<div>
83+
<ChildComponent style={childStyle} />
84+
<button onClick={() => setCount(count + 1)}>Increment</button>
85+
</div>
86+
)
87+
}
88+
```
89+
90+
- **Inline Functions:** Search for and fix functions defined inline within props. Transform this:
91+
92+
```jsx
93+
<button onClick={() => handleClick()}>Click Me</button>
94+
```
95+
96+
Into this:
97+
98+
```jsx
99+
const handleButtonClick = useCallback(() => handleClick(), [])
100+
// ...
101+
<button onClick={handleButtonClick}>Click Me</button>
102+
```
103+
104+
When dependencies are needed:
105+
106+
```jsx
107+
const handleButtonClick = useCallback(() => handleClick(id), [id])
108+
```
109+
110+
- **Inline Function, Stable Value:** When reviewing memoized functions, verify that the dependency array is correctly managed.
111+
112+
3. **Context Usage:** When examining React Context usage, check if context changes cause widespread rerendering. If found, implement more granular contexts or alternative state management. For example, transform this problematic pattern:
113+
114+
```jsx
115+
const BigContext = React.createContext()
116+
117+
function Provider({ children }) {
118+
const [frequently, setFrequently] = useState(0)
119+
const [rarely, setRarely] = useState(0)
120+
121+
const value = { frequently, rarely, setFrequently, setRarely }
122+
123+
return (
124+
<BigContext.Provider value={value}>
125+
{children}
126+
</BigContext.Provider>
127+
)
128+
}
129+
```
130+
131+
Into this optimized version:
132+
133+
```jsx
134+
const FrequentContext = React.createContext()
135+
const RareContext = React.createContext()
136+
137+
function Provider({ children }) {
138+
const [frequently, setFrequently] = useState(0)
139+
const [rarely, setRarely] = useState(0)
140+
141+
const frequentValue = useMemo(() => ({ frequently, setFrequently }), [frequently])
142+
const rareValue = useMemo(() => ({ rarely, setRarely }), [rarely])
143+
144+
return (
145+
<FrequentContext.Provider value={frequentValue}>
146+
<RareContext.Provider value={rareValue}>
147+
{children}
148+
</RareContext.Provider>
149+
</FrequentContext.Provider>
150+
)
151+
}
152+
```
153+
154+
</Unnecessary Rerenders>
155+
156+
<Virtual DOM and Reconciliation>
157+
158+
When optimizing code, remember that React's rerendering process (running the component's function and performing the virtual DOM diff) is separate from actual DOM updates. While a rerender doesn't always mean a DOM update, unnecessary rerenders still consume computational resources and should be eliminated through the patterns above.
159+
160+
</Virtual DOM and Reconciliation>
161+
162+
When analyzing and modifying code, structure the changes as follows:
163+
164+
- **Problem Identification:** Identify the specific performance issue in the code.
165+
- **Code Modification:** Apply the appropriate optimization pattern from above.
166+
- **Verification:** Explain how the modification prevents unnecessary rerenders.
167+
- **Testing:** Verify the optimization through React DevTools and performance monitoring.
168+
169+
Continue this process for each component or section of code being optimized.

0 commit comments

Comments
 (0)