-
Notifications
You must be signed in to change notification settings - Fork 39
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
ReScript-aware memo
#111
Comments
I wouldn't say that the current binding is incorrect. @module("react")
external memo: component<'props> => component<'props> = "memo"
@module("react")
external memoCustomCompareProps: (
component<'props>,
@uncurry ('props, 'props) => bool,
) => component<'props> = "memo" This seems fine to me, although in ReScript 11 Uncurried Mode we could have a single function @module("react")
external memo: (
component<'props>,
~arePropsEqual: ('props, 'props) => bool=?,
) => component<'props> = "memo" So the user can pass in an optional equality function that fits his use case, e.g., if one of the props is a variant (that is not I don't really understand the "recursive shallow-equal" thing - isn't that a deep equal then, and doesn't it also come with quite a bit of performance overhead? |
With a bit of reasoning, I think we can avoid a full comparison of values by only doing recursion on known wrapper structures like Or we could add something like |
Just opened rescript-lang/rescript#6738 as a minimal building block of this |
The current definition of
React.memo
is:which is incorrect.
React.memo
takes an equality function as an optional second paramhttps://react.dev/reference/react/memo
Fortunately, this can be easily fixed without breaking
Customizing the second argument is rare in regular JS/TS projects, but not in ReScript.
ReScript's powerful type system represents boxed objects at runtime. Ironically, this makes
memo
almost useless in ReScript projects, since the default for memo compares shallow equality.E.g. Playground
Users can easily make deep-equality function
generates
However the deep-equal implementation is usually not what React users want. This will be a huge overhead when dealing with data that is not a persistent structure.
Assuming the user still wants the shallow-equal, we can try a much more optimized solution. The idea is simple, making recursive shallow-equal, using well-known structures.
builtin
shallowEqual
function in Reactmodified for ReScript outputs
it seems like compiler support is needed first 😅
The text was updated successfully, but these errors were encountered: