Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Update v4 spec about sharing props type #701

Merged
merged 5 commits into from
Oct 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions cli/JSXV4.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,51 @@ let p: A.props<_> = {x: "x", y: "y"}
<A x="X" {...p}>
<A {...p} {...p1}>
```

### Shared props type (new feature)

V4 introduces support to control the definition of the `props` type by passing as argument to `@react.component` the body of the type definition of `props`. The main application is sharing a single type definition across several components. Here are a few examples:


```rescript
type sharedprops<'x, 'y> = {x: 'x, y: 'y, z:string}

module C1 = {
@react.component(:sharedProps<'a, 'b>)
let make = (~x, ~y) => React.string(x ++ y ++ z)
}

module C2 = {
@react.component(:sharedProps<string, 'b>)
let make = (~x, ~y) => React.string(x ++ y ++ z)
}

module C3 = {
type myProps = sharedProps<int, int>
@react.component(:myProps)
let make = (~x, ~y) => React.int(x + y)
}
```

The generated code (some details removed) looks like this:
```rescript
@@jsxConfig({version: 4, mode: "classic"})

type sharedprops<'x, 'y> = {x: 'x, y: 'y, z: string}

module C1 = {
type props<'a, 'b> = sharedProps<'a, 'b>
let make = ({x, y, _}: props<_>) => React.string(x ++ y ++ z)
}

module C2 = {
type props<'b> = sharedProps<string, 'b>
let make = ({x, y, _}: props<_>) => React.string(x ++ y ++ z)
}

module C3 = {
type myProps = sharedProps<int, int>
type props = myProps
let make = ({x, y, _}: props) => React.int(x + y)
}
```