fix: remove [@mel.uncurry] from React.useCallback* functions#786
Conversation
let foo = React.useCallback((_a, _b) => Js.log("HELLO"));Maybe this could be added to the tests. |
af9c809 to
b8c0f2f
Compare
|
I tried to understand better why this change works, and why it doesn't break any other existing cases. Leaving here a note for the record. The reason is that
The above makes me wonder about the |
This isn't true. I caught the bug when passing it to JavaScript, e.g.: module X = {
[@mel.module "foobar"] [@react.component]
external make: (~takesFunction: (a, b) => unit) => React.element = "default";
};
module FromOCaml = {
[@react.component]
let make = () => {
let myFn = React.useCallback0((a,b)=>a+b);
<X takesFunction=myFn />;
};
}; |
|
I think in the case you mention, the I think it breaks even without |
|
That's true. Are you suggesting we remove |
|
It's also not possible to use uncurried functions in |
|
So perhaps the module Uncurried = {
[@mel.module "react"]
external useCallback1_0:
((. 'input) => 'output, [@mel.as {json|[]|json}] _) =>
((. 'input) => 'output) =
"useCallback";
[@mel.module "react"]
external useCallback2_0:
((. 'input1, 'input2) => 'output, [@mel.as {json|[]|json}] _) =>
((. 'input1, 'input2) => 'output) =
"useCallback";
};
|
Not really. With the message above I just wanted to express that the code in the I am not sure about the support for uncurried functions... I need to think about it more. |
|
I think we could simplify things quite a bit if we get rid of the function type on the I know this might sound unsafe, but ReactJS itself doesn't care about whatever is on the value passed to [@mel.module "react"]
external useCallback0: ('a, [@mel.as {json|[]|json}] _) => 'a = "useCallback";
let x = useCallback0((. a, b) => a + b);
let y = x(. 2, 3); |
|
Seems more correct than the current situation anyway. |
|
@jchavarri used your suggestion. Added a generic |
|
It's minor but there's a runtime check of being sure that useCallback gets a function. That's probably why the callback type was added in the first place. |
Do you mean in React.js proper? That's even better, then. React can make sure we're passing a function (though it'll be caught at development time). |
|
Seems React just swallows whatever is passed to it: https://stackblitz.com/edit/react-playground-practice-13ux64?file=index.js |
6557435 to
c2af1cd
Compare
|
@jchavarri I got a TS error on JSX and didn't realised useCallback eats everything. |
CHANGES: * Remove legacy `ReactDOMRe` and `ReasonReact` modules (@anmonteiro in [reasonml/reason-react#782](reasonml/reason-react#782)) * Fix `React.useCallback*` for callbacks with multiple arguments (@anmonteiro in [reasonml/reason-react#786](reasonml/reason-react#786))
* [new release] reason-react (2 packages) (0.13.0) CHANGES: * Remove legacy `ReactDOMRe` and `ReasonReact` modules (@anmonteiro in [reasonml/reason-react#782](reasonml/reason-react#782)) * Fix `React.useCallback*` for callbacks with multiple arguments (@anmonteiro in [reasonml/reason-react#786](reasonml/reason-react#786)) * Add melange v2 and remove upper bound on ocaml * Remove upper bound in ocaml * Use {post} in reason-react-ppx to bypass check and keep runtests * Remove {post} and remove runtest from building * Remove runtests from rr * Update packages/reason-react-ppx/reason-react-ppx.0.13.0/opam --------- Co-authored-by: Kate <kit-ty-kate@outlook.com>
* [new release] reason-react (2 packages) (0.13.0) CHANGES: * Remove legacy `ReactDOMRe` and `ReasonReact` modules (@anmonteiro in [reasonml/reason-react#782](reasonml/reason-react#782)) * Fix `React.useCallback*` for callbacks with multiple arguments (@anmonteiro in [reasonml/reason-react#786](reasonml/reason-react#786)) * Add melange v2 and remove upper bound on ocaml * Remove upper bound in ocaml * Use {post} in reason-react-ppx to bypass check and keep runtests * Remove {post} and remove runtest from building * Remove runtests from rr * Update packages/reason-react-ppx/reason-react-ppx.0.13.0/opam --------- Co-authored-by: Kate <kit-ty-kate@outlook.com>

React.useCallbackcan take a function of multiple arguments. In the following example:The logging never occurs with
[@mel.uncurry]because Melange generates the following code:We preserve the uncurried callbacks in
React.Uncurried.useCallback*