Skip to content

Commit c11b357

Browse files
committed
Simplify arePropsEqual sample
1 parent 07a2194 commit c11b357

File tree

1 file changed

+20
-28
lines changed

1 file changed

+20
-28
lines changed

pages/docs/react/latest/memo.mdx

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,51 +48,43 @@ var make = React.memo(function (props) {
4848
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.
4949
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.
5050

51-
In ReScript, to use the `arePropsEqual` function, you must redefine the `memo` binding.
51+
In ReScript, to use the `arePropsEqual` function, you must redefine the `make` binding with `React.memoCustomCompareProps`.
5252

5353
<CodeTab>
5454

5555
```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) => {
56+
@react.component
57+
let make = (~disabled, ~onClick) => {
6858
<button
6959
disabled={disabled}
7060
onClick={ev => ev->JsxEvent.Mouse.preventDefault->onClick}>
7161
{React.string("My button")}
7262
</button>
73-
})
63+
}
64+
65+
let make = React.memoCustomCompareProps(make, (p1, p2) =>
66+
p1.disabled == p2.disabled
67+
)
7468
```
7569

7670
```js
7771
import * as React from "react";
7872
import * as JsxRuntime from "react/jsx-runtime";
7973

80-
function memo(__x) {
81-
return React.memo(__x, (function (p1, p2) {
82-
return p1.disabled === p2.disabled;
83-
}));
74+
function Playground(props) {
75+
var onClick = props.onClick;
76+
return JsxRuntime.jsx("button", {
77+
children: "My button",
78+
disabled: props.disabled,
79+
onClick: (function (ev) {
80+
onClick((ev.preventDefault(), undefined));
81+
})
82+
});
8483
}
8584

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-
});
85+
var make = React.memo(Playground, (function (p1, p2) {
86+
return p1.disabled === p2.disabled;
87+
}));
9688
```
9789

9890
</CodeTab>

0 commit comments

Comments
 (0)