Skip to content

Add Jsx.addKeyProp function #5664

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

Merged
merged 2 commits into from
Sep 17, 2022
Merged

Add Jsx.addKeyProp function #5664

merged 2 commits into from
Sep 17, 2022

Conversation

mununki
Copy link
Member

@mununki mununki commented Sep 16, 2022

This PR adds Jsx.addKeyProp in order to add the value of key into the props for React API. This function is needed to pass the type checking to the React component's props type which doesn't have key anymore. rescript-lang/syntax#635

// original
<Foo key="k" x="x" />

// converted
React.createElement(Foo.make, Jsx.addKeyProp(({x: "x"}: Foo.props<_>), "k")),
//                            ^^^^^^^^^^^^^^


let addKeyProp (o : 't) k =
Obj.magic (Js.Obj.assign (Obj.magic o) [%obj { key = k }])
[@@inline]
Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure whether [@@inline] is correct or not. I'm wondering [@@bs.inline] is current, but it fails to be compiled. Unused attribute ... error is thrown.

Copy link
Member Author

Choose a reason for hiding this comment

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

Js output seems incorrect either.

// generated
Jsx.addKeyProp({ x: "x" }, { key: "k1"})

// expected
Object.assign({ x: "x" }, { key: "k1"})

Copy link
Collaborator

Choose a reason for hiding this comment

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

Same:

let is_inline : attr -> bool =
 fun ({ txt }, _) -> txt = "bs.inline" || txt = "inline"

I think it's more like inline is not so well specified. Not even sure it works across files.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I would assume a bundler would inline it anyway?

Copy link
Member Author

Choose a reason for hiding this comment

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

I would assume a bundler would inline it anyway?

Worth checking. The current output is working fine without any issues in the example project anyway.

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK to merge?

@cristianoc cristianoc merged commit 427151e into master Sep 17, 2022
@cristianoc cristianoc deleted the jsx-add-key-prop branch September 17, 2022 05:03
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