Why does ReScript React use labeled arguments instead of props?

I just saw this in the ReScript React docs. What are the reasons for this choice ? Record types seem easier to work with, can use the spread syntax, facilitate HOC and are the common way to work in React. And why is @react.component needed?

Can anyone explain?

In ReactJS, props are usually described as a single props record. In ReScript, we use labeled arguments to define our props parameters instead.

Some guesses:

  • optional parameters (optional fields in records weren’t able a few versions ago)
  • performance (just pass parameters instead of creating objects, but probably the compiled code will be the same)

Btw.: if you create a make function with a props record as parameter, you don’t need @react.component or @jsx.component
I use it only for components without props.

There is some context on the design. [RFC] Optionally labeled arguments · Issue #6646 · rescript-lang/rescript-compiler · GitHub

There is also RFC: `@deriving` for labeled arguments · Issue #6530 · rescript-lang/rescript-compiler · GitHub

Or maybe your question is React-specific.

In JSX v4, you can use record types for sharing.

But still, labeled arguments can provide better ergonomics. It allows for co-locating type definitions and inference without having to duplicate declarations.

This is a feature that TypeScript people are really envious of, and is also a proposal for the Component DSL that Flow recently added.

We’re already ahead of the curve :wink: