Skip to content

Commit 9df10a0

Browse files
authored
Add page about React.memo (#889)
* Add page about React.memo * Simplify arePropsEqual sample * Uppercase description start. * Show options to use memoCustomCompareProps
1 parent f831d59 commit 9df10a0

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

data/sidebar_react_latest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"events",
1313
"refs-and-the-dom",
1414
"context",
15+
"memo",
1516
"styling",
1617
"router",
1718
"lazy-components",

pages/docs/react/latest/memo.mdx

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
---
2+
title: memo
3+
description: "Using React.memo"
4+
canonical: "/docs/react/latest/memo"
5+
---
6+
7+
# memo
8+
9+
`React.memo` lets you skip re-rendering a component when its props are unchanged.
10+
11+
Wrap a component in memo to get a memoized version of that component.
12+
This memoized version of your component will usually not be re-rendered when its parent component is re-rendered as long as its props have not changed.
13+
14+
<small>But React may still re-render it: memoization is a performance optimization, not a guarantee.</small>
15+
16+
<CodeTab labels={["ReScript", "JS Output"]}>
17+
18+
```res
19+
@react.component
20+
let make = React.memo((~a: int, ~b: string) => {
21+
<div>
22+
{React.int(a)}
23+
<br />
24+
{React.string(b)}
25+
</div>
26+
})
27+
```
28+
29+
```js
30+
import * as React from "react";
31+
import * as JsxRuntime from "react/jsx-runtime";
32+
33+
var make = React.memo(function (props) {
34+
return JsxRuntime.jsxs("div", {
35+
children: [
36+
props.a,
37+
JsxRuntime.jsx("br", {}),
38+
props.b
39+
]
40+
});
41+
});
42+
```
43+
44+
</CodeTab>
45+
46+
## arePropsEqual
47+
48+
In React, memo can accept an optional argument called "arePropsEqual". This function takes two arguments: the previous props and the new props of the component.
49+
It should return true if the old and new props are the same, meaning the component will produce the same output and behavior with the new props as it did with the old ones.
50+
51+
In ReScript, you can use the `arePropsEqual` function with the `React.memoCustomCompareProps` binding. However, `React.memoCustomCompareProps` cannot be combined with `@react.component`.
52+
53+
To work around this, you can redefine the make binding:
54+
55+
<CodeTab labels={["ReScript", "JS Output"]}>
56+
57+
```res
58+
@react.component
59+
let make = (~disabled, ~onClick) => {
60+
<button
61+
disabled={disabled}
62+
onClick={ev => ev->JsxEvent.Mouse.preventDefault->onClick}>
63+
{React.string("My button")}
64+
</button>
65+
}
66+
67+
let make = React.memoCustomCompareProps(make, (p1, p2) =>
68+
p1.disabled == p2.disabled
69+
)
70+
```
71+
72+
```js
73+
import * as React from "react";
74+
import * as JsxRuntime from "react/jsx-runtime";
75+
76+
function Playground(props) {
77+
var onClick = props.onClick;
78+
return JsxRuntime.jsx("button", {
79+
children: "My button",
80+
disabled: props.disabled,
81+
onClick: (function (ev) {
82+
onClick((ev.preventDefault(), undefined));
83+
})
84+
});
85+
}
86+
87+
var make = React.memo(Playground, (function (p1, p2) {
88+
return p1.disabled === p2.disabled;
89+
}));
90+
```
91+
92+
</CodeTab>
93+
94+
95+
Another approach is to use a custom prop type and remove the `@react.component` annotation.
96+
97+
<CodeTab labels={["ReScript", "JS Output"]}>
98+
99+
```res
100+
type props = {
101+
disabled: bool,
102+
onClick: JsxEvent.Mouse.t => unit,
103+
}
104+
105+
let make = React.memoCustomCompareProps(
106+
({disabled, onClick}) => {
107+
<button disabled={disabled} onClick={ev => ev->onClick}>
108+
{React.string("My button")}
109+
</button>
110+
},
111+
(p1, p2) => p1.disabled == p2.disabled,
112+
)
113+
```
114+
115+
```js
116+
import * as React from "react";
117+
import * as JsxRuntime from "react/jsx-runtime";
118+
119+
var make = React.memo((function (param) {
120+
var onClick = param.onClick;
121+
return JsxRuntime.jsx("button", {
122+
children: "My button",
123+
disabled: param.disabled,
124+
onClick: (function (ev) {
125+
onClick(ev);
126+
})
127+
});
128+
}), (function (p1, p2) {
129+
return p1.disabled === p2.disabled;
130+
}));
131+
```
132+
133+
</CodeTab>

0 commit comments

Comments
 (0)