-
-
Notifications
You must be signed in to change notification settings - Fork 248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add page about React.memo #889
Conversation
@nojaf is attempting to deploy a commit to the ReScript Association Team on Vercel. A member of the Team first needs to authorize it. |
@fhammerschmidt any suggestions? |
(Also, apologies for my impatience 😅) |
I think the
Playground Link |
Oh that does seem simpler. My original example was based on https://forum.rescript-lang.org/t/looking-for-a-react-memo-example/1144/12?u=nojaf I'll revisit the docs. |
pages/docs/react/latest/memo.mdx
Outdated
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. | ||
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. | ||
|
||
In ReScript, to use the `arePropsEqual` function, you must redefine the `make` binding with `React.memoCustomCompareProps`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about "must" here, because, as you demonstrated, there are other ways as well.
Maybe "you can redefine the make
binding"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a bit of "must" in the sense that you cannot just write:
So, you must do something other than that to get it to work. Which is the exact thing I want to add to the documentation, as that was confusing to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other thing you can do is to not use the JSX preprocessor extension at all btw (i.e. no @react.component
annotation). But that once again requires explicitly typing props.
type props = {
disabled: bool,
onClick: JsxEvent.Mouse.t => unit,
}
let make = React.memoCustomCompareProps(
({disabled, onClick}) => {
<button
disabled={disabled}
onClick={ev => ev->JsxEvent.Mouse.preventDefault->onClick}>
{React.string("My button")}
</button>
},
(p1, p2) => p1.disabled == p2.disabled,
)
so maybe we can keep the "must", but give an alternative solution
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I rephrased it to emphasize you can't combine @react.component
and React.memoCustomCompareProps
. Added the two workarounds as example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For my own understanding, does @react.component
do anything in terms of using the component in JSX syntax? Or is that not a requirement?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since JSX4, what the annotation does is basically generating a props type (probably with more generic types though). So no, using them is identical.
Have a look at the blogpost that announced JSX V4 in ReScript 10.1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, thank you!
Nice work! |
A lot of text comes from https://react.dev/reference/react/memo#memo.
The important thing for me is the
arePropsEqual
example.Let me know if you like to see anything different.