Skip to content

Add jsx ppx for attribute @react.componentWithProps - #7203

Merged
mununki merged 6 commits into
masterfrom
jsx-ppx-with-props
Dec 18, 2024
Merged

mununki merged 6 commits into
masterfrom
jsx-ppx-with-props

Conversation

@mununki

@mununki mununki commented Dec 15, 2024 •

Copy link
Copy Markdown
Member

This PR introduces a new @react.componentWithProps attribute that explicitly handles the props with shared props:

// source
type props = sharedProps
@react.componentWithProps
let make = props => React.string(props.x)

// transformed to
type props = sharedProps
let make = props => React.string(props.x)
let make = {
  let \"SharedPropsWithProps" = props => make(props)
  \"SharedPropsWithProps"
}

@github-actions github-actions Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Syntax Benchmarks'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.05.

Benchmark suite Current: 998282d Previous: e1b7fb7 Ratio
Print RedBlackTree.res - time/run 2.5336877066666665 ms 2.3104395133333333 ms 1.10
Print RedBlackTreeNoComments.res - time/run 2.3414450266666664 ms 2.10057036 ms 1.11
Print Napkinscript.res - time/run 81.58969135333332 ms 77.00100409999999 ms 1.06
Print HeroGraphic.res - time/run 9.61578382 ms 8.775952553333333 ms 1.10

This comment was automatically generated by workflow using github-action-benchmark.

@cknitt cknitt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great work! Thanks a lot! Could you add a CHANGELOG entry?

@mununki

mununki commented Dec 15, 2024

Copy link
Copy Markdown
Member Author

Great work! Thanks a lot! Could you add a CHANGELOG entry?

Sure.
I will request another review after adding warnings for unsupported use cases. One notable example would be the discouraged forwardRef usage cases.

@mununki
mununki requested a review from cknitt December 18, 2024 06:34

@cknitt cknitt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks again!

@mununki
mununki merged commit f2fd5e1 into master Dec 18, 2024
@cknitt
cknitt deleted the jsx-ppx-with-props branch December 22, 2024 13:05
@dkirchhof

Copy link
Copy Markdown
Contributor

Does it also work for bindings?
Tried it out but observed this behavior:

Old syntax:

@module("react-konva") @jsx.component(: props)
external make: props => Jsx.element = "Rect"
JsxRuntime.jsx(ReactKonva.Rect, {
  x: rect.x,
  y: rect.y,
  width: rect.width,
  height: rect.height,
  fill: "black"
}, key);

New syntax (doesn't work):

@module("react-konva") @jsx.componentWithProps
external make: props => Jsx.element = "Rect"
JsxRuntime.jsx(prim => ReactKonva.Rect(prim), {
  x: rect.x,
  y: rect.y,
  width: rect.width,
  height: rect.height,
  fill: "black"
}, key);

@mununki

mununki commented Dec 24, 2024

Copy link
Copy Markdown
Member Author

Does it also work for bindings? Tried it out but observed this behavior:

Old syntax:

@module("react-konva") @jsx.component(: props)
external make: props => Jsx.element = "Rect"
JsxRuntime.jsx(ReactKonva.Rect, {
  x: rect.x,
  y: rect.y,
  width: rect.width,
  height: rect.height,
  fill: "black"
}, key);

New syntax (doesn't work):

@module("react-konva") @jsx.componentWithProps
external make: props => Jsx.element = "Rect"
JsxRuntime.jsx(prim => ReactKonva.Rect(prim), {
  x: rect.x,
  y: rect.y,
  width: rect.width,
  height: rect.height,
  fill: "black"
}, key);

Oh, I think we need to add the syntax error for external bindings for @react.componentWithProps. Thank you for catching this.

BTW, the old one seems the proper binding for your case. @react.comnponentWithProps is for the definition of components except the external bindings.

@dkirchhof

Copy link
Copy Markdown
Contributor

So no chance to get it also working for external bindings? Asking just for consistency.

@nojaf

nojaf commented Aug 31, 2025

Copy link
Copy Markdown
Collaborator

the old one seems the proper binding for your case. @react.comnponentWithProps is for the definition of components except the external bindings.

@mununki is there any reason you didn't went with supporting @react.component(:propType) for non external bindings instead?
Could that not have done the same as @react.componentWithProps does right now?

aspeddro pushed a commit to aspeddro/rescript-compiler that referenced this pull request Oct 31, 2026
…7203)

* add ppx for attribute @react.componentWithProps

* clean up the attribute after transformation

* use fn_name

* add arity=1

* build error using @react.componentWithProps with React.forwardRef

* change log

Signed-off-by: Pedro Castro <aspeddro@gmail.com>
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.

4 participants