Skip to content
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

JSX: error with fragment after fixing children type #6234

Closed
cknitt opened this issue May 2, 2023 · 7 comments · Fixed by #6238
Closed

JSX: error with fragment after fixing children type #6234

cknitt opened this issue May 2, 2023 · 7 comments · Fixed by #6238
Assignees

Comments

@cknitt
Copy link
Member

cknitt commented May 2, 2023

rescript-lang/rescript-react#94 fixes the children type for <>, React.Fragment, React.StrictMode and React.Suspense by making it React.element instead of 'children.

This causes an issue with <>:

<>
  <A />
  <B />
</>

now gives the following error without location:

This has type: array<'a>
  Somewhere wanted: React.element (defined as JsxU.element)

The components React.Fragment, React.StrictMode and React.Suspense still work fine though, so it seems there is some special handling for <> in the PPX that needs to be adapted.

@mununki
Copy link
Member

mununki commented May 2, 2023

I'll look into it later tonight! Thanks!

@mununki
Copy link
Member

mununki commented May 2, 2023

Have a quick look, and found there is a different transformation process for <> </> from other react components. I think there is no need to be special for that fragment expression against <React.Fragment> </React.Fragment>.
Unifying it would be a fix for this issue.

@mununki
Copy link
Member

mununki commented May 2, 2023

a bit weirder.

module A = {
  @react.component
  let make = () => {
    <>
      <div />
    </>
  }
}
This has type:
    React.component<React.fragmentProps> (defined as
      React.fragmentProps => Jsx.element)
  Somewhere wanted: string

@cknitt
Copy link
Member Author

cknitt commented May 2, 2023

That's in JSX 4 classic mode, right? I was using automatic mode.

@mununki
Copy link
Member

mununki commented May 2, 2023

That's in JSX 4 classic mode, right? I was using automatic mode.

Yes. JSX 4 classic and automatic has different build error each. I'll write down the summary.

@mununki
Copy link
Member

mununki commented May 2, 2023

Here are the summarized issues regarding the shorthand syntax <> </>:

  1. JSX4 automatic: As per the AST after JSX4 transformation, React.array is missing. Having a single child is fine.
<> <div /> <div /> </>
  1. JSX4 classic with a single child: It doesn't match the React.createElement signature. It should have Pexp_record [ "children", ...] instead of Pexp_array []
<> <div /> </>
  1. JSX4 classic with a multiple children: It doesn't match the React.createElement signature. It needs to use React.createElementVariadic as others.
<> <div /> <div /> </>
  1. JSX4 classic: transformed to ReactDOM.createElement* instead of React.createElement* That is why the error message complains about expecting string.

@mununki
Copy link
Member

mununki commented May 2, 2023

I don't feel the need to change the type definition of the fragment for JSX3 or modify the ppx yet, as long as working fine.

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 a pull request may close this issue.

2 participants