Skip to content
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

Merged
merged 4 commits into from
Jun 19, 2024
Merged

Conversation

nojaf
Copy link
Contributor

@nojaf nojaf commented Jun 18, 2024

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.

Copy link

vercel bot commented Jun 18, 2024

@nojaf is attempting to deploy a commit to the ReScript Association Team on Vercel.

A member of the Team first needs to authorize it.

@nojaf
Copy link
Contributor Author

nojaf commented Jun 19, 2024

@fhammerschmidt any suggestions?

@nojaf
Copy link
Contributor Author

nojaf commented Jun 19, 2024

(Also, apologies for my impatience 😅)

@fhammerschmidt
Copy link
Member

I think the arePropsEqual example can be simplified by shadowing the make function:

@react.component
let make = (~disabled, ~onClick) => {
  <button
    disabled={disabled}
    onClick={ev => ev->JsxEvent.Mouse.preventDefault->onClick}>
    {React.string("My button")}
  </button>
}

let make = React.memoCustomCompareProps(make, (p1, p2) =>
  p1.disabled == p2.disabled
)

Playground Link
no explicit typing of props needed either, but I never used that personally.

@nojaf
Copy link
Contributor Author

nojaf commented Jun 19, 2024

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 Show resolved Hide resolved
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`.
Copy link
Member

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"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thank you!

@fhammerschmidt fhammerschmidt merged commit 9df10a0 into rescript-lang:master Jun 19, 2024
1 of 2 checks passed
@fhammerschmidt
Copy link
Member

Nice work!

@nojaf nojaf deleted the react-memo branch June 19, 2024 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants