Skip to content

feat(compiler-plugins): emit JSX for ReactComponent calls - #724

Open
MangelMaxime wants to merge 2 commits into
mainfrom
feature/jsx-react-component-calls
Open

MangelMaxime wants to merge 2 commits into
mainfrom
feature/jsx-react-component-calls

Conversation

@MangelMaxime

@MangelMaxime MangelMaxime commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Important

This only works with Fable 5.24.0. Earlier versions ignore Tags = [ "jsx-spread" ], so record props would not spread into the JSX element. The PR bumps the Fable tool in .config/dotnet-tools.json to 5.24.0.

When compiling with -e .jsx, calls to [<ReactComponent>] functions still came out as createElement calls inside the JSX:

<div>
    {createElement(Greeting, { name: "a" })}
    {createElement(Wrapper, { children: ofArray([createElement(Greeting, { name: "b" })]) })}
</div>

Now they come out as elements:

<div>
    <Greeting name="a" />
    <Wrapper>
        <Greeting name="b" />
    </Wrapper>
</div>

Same for memo and lazy components. .js output doesn't change.

The one thing worth a close look is children. Real JSX children arrive as nothing, one element or a JS array, so this component was silently printing count=0:

[<ReactComponent>]
let Wrapper (children: ReactElement list) =
  Html.div [ prop.text (sprintf "count=%d" (List.length children)) ]

The declaration now converts $props.children back to the declared type:

export function Wrapper($props) {
  const children = $props.children instanceof FSharpList ? $props.children
      : ofArray(Array.isArray($props.children) ? $props.children
                : $props.children == null ? [] : [$props.children]);
  ...
}

Array and seq get the same treatment without the ofArray. A bare ReactElement is left alone. ReactComponentChildren.test.fs covers the three types with two, one, zero and runtime-built children.

The lazy-by-reference code only recognised createElement calls, so it was taught the new form too.

Record props use JSX.spread, which shipped in Fable 5.24.0 (fable-compiler/Fable#5141). The full suite passes against it, 146 tests.

No new attribute flag: if you compile to .jsx you get JSX. Can add an opt-out later if anyone needs it.

This branch has not been deployed

No deployments
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.

1 participant