Skip to content

Commit cbb095c

Browse files
committed
main 🧊 add memo article
1 parent 5ad9109 commit cbb095c

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Memoization
2+
3+
## Library Philosophy
4+
5+
ReactUse **deliberately avoids memoizing** hook methods by default. This decision is based on several important principles of React application development.
6+
7+
## About optimization
8+
9+
> "Premature optimization is the root of all evil" β€” Donald Knuth
10+
11+
Memoization is an optimization technique, and like any optimization, it should be applied consciously when there's a real performance problem. Automatic memoization of all functions can:
12+
13+
- **Hide real architectural problems** β€” instead of fixing the root cause of unnecessary re-renders, we mask the symptoms
14+
- **Create a false sense of security** β€” developers might think performance is already optimized
15+
- **Complicate debugging** β€” memoized functions don't behave as expected when dependencies change
16+
17+
## Re-renders are normal
18+
19+
React is designed so that component re-renders are a natural and efficient part of its operation. Modern browsers and React Fiber can handle a lot of re-renders without noticeable impact on performance.
20+
21+
Memoization is a tool that should be in the hands of the application developer, not the library.
22+
23+
```tsx
24+
import { useCounter } from '@siberiacancode/reactuse';
25+
import { useCallback, useMemo } from 'react';
26+
27+
function ExpensiveComponent() {
28+
const counter = useCounter(0);
29+
30+
const expensiveValue = useMemo(() => {
31+
return performHeavyCalculation(counter.value);
32+
}, [counter.value]);
33+
34+
const handleIncrement = useCallback(() => {
35+
performHeavyOperation();
36+
counter.inc();
37+
}, [counter.inc]);
38+
39+
// ...
40+
}
41+
```
42+
43+
## React compiler
44+
45+
The React team is working on **React Compiler** β€” a tool that will automatically optimize your code by adding memoization where it's truly needed.
46+
47+
When React Compiler becomes stable, the question of manual memoization will become less relevant, but the principles of conscious optimization will remain important.

0 commit comments

Comments
Β (0)