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

Commit 7b2ed1b

Browse files
mununkicristianoc
andauthored
Update v4 spec about sharing props type (#701)
* update v4 spec about sharing props * fix the example of shared props for v4 spec * clean up the spec about shared props * Update JSXV4.md * update jsx v4 spec about shared props constraint Co-authored-by: Cristiano Calcagno <[email protected]>
1 parent 3f8eb8b commit 7b2ed1b

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

cli/JSXV4.md

+48
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,51 @@ let p: A.props<_> = {x: "x", y: "y"}
373373
<A x="X" {...p}>
374374
<A {...p} {...p1}>
375375
```
376+
377+
### Shared props type (new feature)
378+
379+
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:
380+
381+
382+
```rescript
383+
type sharedprops<'x, 'y> = {x: 'x, y: 'y, z:string}
384+
385+
module C1 = {
386+
@react.component(:sharedProps<'a, 'b>)
387+
let make = (~x, ~y) => React.string(x ++ y ++ z)
388+
}
389+
390+
module C2 = {
391+
@react.component(:sharedProps<string, 'b>)
392+
let make = (~x, ~y) => React.string(x ++ y ++ z)
393+
}
394+
395+
module C3 = {
396+
type myProps = sharedProps<int, int>
397+
@react.component(:myProps)
398+
let make = (~x, ~y) => React.int(x + y)
399+
}
400+
```
401+
402+
The generated code (some details removed) looks like this:
403+
```rescript
404+
@@jsxConfig({version: 4, mode: "classic"})
405+
406+
type sharedprops<'x, 'y> = {x: 'x, y: 'y, z: string}
407+
408+
module C1 = {
409+
type props<'a, 'b> = sharedProps<'a, 'b>
410+
let make = ({x, y, _}: props<_>) => React.string(x ++ y ++ z)
411+
}
412+
413+
module C2 = {
414+
type props<'b> = sharedProps<string, 'b>
415+
let make = ({x, y, _}: props<_>) => React.string(x ++ y ++ z)
416+
}
417+
418+
module C3 = {
419+
type myProps = sharedProps<int, int>
420+
type props = myProps
421+
let make = ({x, y, _}: props) => React.int(x + y)
422+
}
423+
```

0 commit comments

Comments
 (0)