Skip to content

Commit d42d557

Browse files
committed
Use a note instead
1 parent 8d95d13 commit d42d557

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/content/learn/react-compiler/introduction.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,28 @@ const ExpensiveComponent = memo(function ExpensiveComponent({ data, onClick }) {
4343
return (
4444
<div>
4545
{processedData.map(item => (
46-
<Item key={item.id} item={item} onClick={handleClick} />
46+
<Item key={item.id} onClick={() => handleClick(item)} />
4747
))}
4848
</div>
4949
);
5050
});
5151
```
5252

53+
54+
<Note>
55+
56+
This manual memoization has a subtle bug that breaks memoization:
57+
58+
```js [[2, 1, "() => handleClick(item)"]]
59+
<Item key={item.id} onClick={() => handleClick(item)} />
60+
```
61+
62+
Even though `handleClick` is wrapped in `useCallback`, the arrow function `() => handleClick(item)` creates a new function every time the component renders. This means that `Item` will always receive a new `onClick` prop, breaking memoization.
63+
64+
The React Compiler is able to optimize this correctly with or without the arrow function, ensuring that `Item` only re-renders when `props.onClick` changes.
65+
66+
</Note>
67+
5368
### After React Compiler {/*after-react-compiler*/}
5469

5570
With React Compiler, you write the same code without manual memoization:
@@ -65,16 +80,16 @@ function ExpensiveComponent({ data, onClick }) {
6580
return (
6681
<div>
6782
{processedData.map(item => (
68-
<Item key={item.id} item={item} onClick={handleClick} />
83+
<Item key={item.id} onClick={() => handleClick(item)} />
6984
))}
7085
</div>
7186
);
7287
}
7388
```
7489

75-
_[See this example in the React Compiler Playground](https://playground.react.dev/#N4Igzg9grgTgxgUxALhAMygOzgFwJYSYAEAogB4AOCmYeAbggMIQC2Fh1OAFMEQCYBDHAIA0RQowA2eOAGsiAXwCURYAB1iROITA4iFGBERgwCPgBEhAogF4iCStVoMACoeO1MAcy6DhSgG4NDSItHT0ACwFMPkkmaTlbIi48HAQWFRsAPlUQ0PFMKRlZFLSWADo8PkC8hSDMPJgEHFhiLjzQgB4+eiyO-OADIwQTM0thcpYBClL02xz2zXz8zoBJMqJZBABPG2BU9Mq+BSIDlj2zk4KiuT2omLib2ROAej6l-KUlBX7Ol566O9QjVMAoQCIQNpMGg8F4UCAQAogA)_
90+
_[See this example in the React Compiler Playground](https://playground.react.dev/#N4Igzg9grgTgxgUxALhAMygOzgFwJYSYAEAogB4AOCmYeAbggMIQC2Fh1OAFMEQCYBDHAIA0RQowA2eOAGsiAXwCURYAB1iROITA4iFGBERgwCPgBEhAogF4iCStVoMACoeO1MAcy6DhSgG4NDSItHT0ACwFMPkkmaTlbIi48HAQWFRsAPlUQ0PFMKRlZFLSWADo8PkC8hSDMPJgEHFhiLjzQgB4+eiyO-OADIwQTM0thcpYBClL02xz2zXz8zoBJMqJZBABPG2BU9Mq+BQKiuT2uTJyomLizkoOMk4B6PqX8pSUFfs7nnro3qEapgFCAFEA)_
7691

77-
React Compiler automatically applies the equivalent optimizations, ensuring your app only re-renders when necessary.
92+
React Compiler automatically applies the optimal memoization, ensuring your app only re-renders when necessary.
7893

7994
<DeepDive>
8095
#### What kind of memoization does React Compiler add? {/*what-kind-of-memoization-does-react-compiler-add*/}

0 commit comments

Comments
 (0)