Skip to content

Commit 07a2194

Browse files
committed
Add page about React.memo
1 parent f831d59 commit 07a2194

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-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: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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, to use the `arePropsEqual` function, you must redefine the `memo` binding.
52+
53+
<CodeTab>
54+
55+
```res
56+
type propsDef = {
57+
disabled: bool,
58+
onClick: unit => unit,
59+
}
60+
61+
// Refine memo to satify the compiler.
62+
let memo = React.memoCustomCompareProps(_, (p1, p2) =>
63+
p1.disabled == p2.disabled
64+
)
65+
66+
@react.component(: propsDef)
67+
let make = memo((~disabled, ~onClick) => {
68+
<button
69+
disabled={disabled}
70+
onClick={ev => ev->JsxEvent.Mouse.preventDefault->onClick}>
71+
{React.string("My button")}
72+
</button>
73+
})
74+
```
75+
76+
```js
77+
import * as React from "react";
78+
import * as JsxRuntime from "react/jsx-runtime";
79+
80+
function memo(__x) {
81+
return React.memo(__x, (function (p1, p2) {
82+
return p1.disabled === p2.disabled;
83+
}));
84+
}
85+
86+
var make = memo(function (props) {
87+
var onClick = props.onClick;
88+
return JsxRuntime.jsx("button", {
89+
children: "My button",
90+
disabled: props.disabled,
91+
onClick: (function (ev) {
92+
onClick((ev.preventDefault(), undefined));
93+
})
94+
});
95+
});
96+
```
97+
98+
</CodeTab>

0 commit comments

Comments
 (0)