You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 13, 2023. It is now read-only.
Repository navigation
This repository was archived by the owner on Nov 13, 2023. It is now read-only.
If you genType a component that forwards ref, the exported typescript component is broken because it tries to call the component as a function. Not sure how to best resolve this as values need to be transformed.
[@genType]
[@react.component]
let input =
React.forwardRef((~children, (), ref) => <View ref> children </View>);
Sorry, my bad. a better example that actually works:
type bla = {text: string};
[@genType]
[@react.component]
let input =
React.forwardRef((~children, ~style, ~bla, ref) =>
<div
ref=?{ref->Js.Nullable.toOption->Belt.Option.map(ReactDOMRe.Ref.domRef)}
?style>
children
{React.string(bla.text)}
</div>
);
(genType calls the function in an outer function to convert the record type, however this is a react component, and with forwardRef it's not a function anymore).
// Generated by BUCKLESCRIPT VERSION 5.0.6, PLEASE EDIT WITH CARE
'use strict';
var React = require("react");
var Belt_Option = require("bs-platform/lib/js/belt_Option.js");
var Caml_option = require("bs-platform/lib/js/caml_option.js");
var input = React.forwardRef((function (Props, ref) {
var children = Props.children;
var style = Props.style;
var bla = Props.bla;
var tmp = { };
var tmp$1 = Belt_Option.map((ref == null) ? undefined : Caml_option.some(ref), (function (prim) {
return prim;
}));
if (tmp$1 !== undefined) {
tmp.ref = Caml_option.valFromOption(tmp$1);
}
if (style !== undefined) {
tmp.style = Caml_option.valFromOption(style);
}
return React.createElement("div", tmp, children, bla[/* text */0]);
}));
exports.input = input;
/* input Not a pure module */
Hopefully ultimately this will be solved by your Bucklescript PR that compiles records to objects instead of arrays, so conversion is not necessary anymore.
No wrapping is performed for components in export position, relying on an invariant: if a component is in export position and requires conversion, then it is a function component (and not a class component)
I think this is an (exotic) counter example. It's also impossible to fix because it's not possible to convert the props without introducing a new component that wraps the original component in this case (which also needs a forwardRef)
A fix could be that if there is a conversion necessary to wrap the component in a component that does the conversion (with a forwardRef). This doesn't make the assumption that a component is always callable as a function.
I think other cases where we could have this issue is a component wrapped in React.memo (not sure about the implementation, perhaps it's still callable as a function)
In the case of React.memo, conversion kills memoization, hence there's no way to save that at the moment (except for this line of exploration rescript-lang/rescript#3785 that aims to make the conversion unnecessary for exactly these cases).
Ah indeed. So genType needs to detect forwardProps and treat it like a non-function component.
In the case of React.memo, conversion kills memoization, hence there's no way to save that at the moment (except for this line of exploration BuckleScript/bucklescript#3785 that aims to make the conversion unnecessary for exactly these cases).
Yeah saw that! I think currently objects are more performant anyway in most browsers with Object.assign, and it makes conversions to JavaScript zero costs, so looking forward when that will land in BuckleScript!
I wish it were possible to detect whether forwardProps was used. The input to genType is the (Reason) type of the function, so it's opaque to how that was obtained.
I think we're in the game of considering always wrapping with React.createElement whenever conversion is required, and ask the question of how this affects perf. The nice thing being, the incentives are aligned towards trying to avoiding conversions.
Thanks will try to implement ASAP and test if it works in our app. Thank you!
genType is really magical btw. So great in a mixed reasonml/typescript app.
Activity
cristianoc commentedon Sep 4, 2019
@jfrolich could you expand a bit with a complete example including the values to be transformed. And the generated file.
jfrolich commentedon Sep 4, 2019
Sorry, my bad. a better example that actually works:
(genType calls the function in an outer function to convert the record type, however this is a react component, and with forwardRef it's not a function anymore).
Below the .gen.tsx result:
and the BuckleScript output:
Hopefully ultimately this will be solved by your Bucklescript PR that compiles records to objects instead of arrays, so conversion is not necessary anymore.
cristianoc commentedon Sep 4, 2019
@jfrolich would you compare with this example:
https://github.com/cristianoc/genType/blob/master/examples/typescript-react-example/src/Hooks.re#L105
See this for a discussion: #226.
Specifically:
You might have a counter-example to that.
cristianoc commentedon Sep 4, 2019
I've also noticed that the example does not type-check if one comments out:
//[@react.component]jfrolich commentedon Sep 4, 2019
I think this is an (exotic) counter example. It's also impossible to fix because it's not possible to convert the props without introducing a new component that wraps the original component in this case (which also needs a
forwardRef)jfrolich commentedon Sep 4, 2019
A fix could be that if there is a conversion necessary to wrap the component in a component that does the conversion (with a forwardRef). This doesn't make the assumption that a component is always callable as a function.
jfrolich commentedon Sep 4, 2019
I think other cases where we could have this issue is a component wrapped in
React.memo(not sure about the implementation, perhaps it's still callable as a function)cristianoc commentedon Sep 4, 2019
Check this example https://github.com/cristianoc/genType/pull/226/files#diff-c4d8a75dfa0fe3d73eec6f3d276241e1R28 of how a non-function component can still be wrapped with conversion.
In the case of
React.memo, conversion kills memoization, hence there's no way to save that at the moment (except for this line of exploration rescript-lang/rescript#3785 that aims to make the conversion unnecessary for exactly these cases).jfrolich commentedon Sep 4, 2019
Ah indeed. So genType needs to detect forwardProps and treat it like a non-function component.
Yeah saw that! I think currently objects are more performant anyway in most browsers with Object.assign, and it makes conversions to JavaScript zero costs, so looking forward when that will land in BuckleScript!
cristianoc commentedon Sep 4, 2019
I wish it were possible to detect whether
forwardPropswas used. The input to genType is the (Reason) type of the function, so it's opaque to how that was obtained.I think we're in the game of considering always wrapping with
React.createElementwhenever conversion is required, and ask the question of how this affects perf. The nice thing being, the incentives are aligned towards trying to avoiding conversions.Wrap export components too in React.createElement when conversion is …
When wrapping with React.createElement, declare separate const for pr…
cristianoc commentedon Sep 5, 2019
@jfrolich see the examples in #249.
In particular
Hooks.re, where I've added your example.Let me know if this addresses your issue.
Wrap export components too in React.createElement when conversion is …
cristianoc commentedon Sep 6, 2019
This is now in release 2.39.0.
Closing optimistically.
jfrolich commentedon Sep 6, 2019
Thanks will try to implement ASAP and test if it works in our app. Thank you!
genType is really magical btw. So great in a mixed reasonml/typescript app.