|
| 1 | +**Abbreviation** |
| 2 | + |
| 3 | +The placement of `@react.component` is an abbreviation as described below. |
| 4 | + |
| 5 | +**Normal Case** |
| 6 | + |
| 7 | +```rescript |
| 8 | +@react.component |
| 9 | +let make = (~x, ~y, ~z) => body |
| 10 | +
|
| 11 | +// is an abbreviation for |
| 12 | +
|
| 13 | +let make = @react.component (~x, ~y, ~z) => body |
| 14 | +``` |
| 15 | + |
| 16 | +**Forward Ref** |
| 17 | + |
| 18 | +```rescript |
| 19 | +@react.component |
| 20 | +let make = React.forwardRef((~x, ~y, ref) => body) |
| 21 | +
|
| 22 | +// is an abbreviation for |
| 23 | +
|
| 24 | +let make = React.forwardRef({ |
| 25 | + let fn = |
| 26 | + @react.component (~x, ~y, ~ref=?) => { |
| 27 | + let ref = ref->Js.Nullable.fromOption |
| 28 | + body |
| 29 | + } |
| 30 | + (props, ref) => fn({...props, ref: {ref->Js.Nullable.toOption}}) |
| 31 | +}) |
| 32 | +``` |
| 33 | + |
| 34 | +**Conversion** |
| 35 | + |
| 36 | +Conversion applies to an arrow function definition where all the arguments are labelled. |
| 37 | +It produces a type definition and a new function. |
| 38 | + |
| 39 | +**Definition** |
| 40 | + |
| 41 | +```rescript |
| 42 | +@react.component (~x, ~y=3+x, ?z) => body |
| 43 | +
|
| 44 | +// is converted to |
| 45 | +
|
| 46 | +type props<'x, 'y, 'z> = {x: 'x, @optional y: 'y, @optional z: 'z, @optional key: string} |
| 47 | +
|
| 48 | +({x, y, z}: props<_>) => { |
| 49 | + let y = switch props.y { |
| 50 | + | None => 3 + x |
| 51 | + | Some(y) => y |
| 52 | + } |
| 53 | + body |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +**Application** |
| 58 | + |
| 59 | +```rescript |
| 60 | +<Comp x> |
| 61 | +// is converted to |
| 62 | +React.createElement(Comp.make, {x}) |
| 63 | +
|
| 64 | +<Comp x y=7 ?z> |
| 65 | +// is converted to |
| 66 | +React.createElement(Comp.make, {x, y:7, @optional z}) |
| 67 | +
|
| 68 | +<Comp x key="7"> |
| 69 | +// is converted to |
| 70 | +React.createElement(Comp.make, {x, key: "7"}) |
| 71 | +``` |
| 72 | + |
| 73 | +**Interface And External** |
| 74 | + |
| 75 | +```rescript |
| 76 | +@react.component (~x: int, ~y: int=?, ~z: int=?) => React.element |
| 77 | +
|
| 78 | +// is converted to |
| 79 | +
|
| 80 | +type props<'x, 'y, 'z> = {x: 'x, @optional y: 'y, @optional z: 'z} |
| 81 | +
|
| 82 | +props<int, int, int> => React.element |
| 83 | +``` |
| 84 | + |
| 85 | +Since an external is a function declaration, it follows the same rule. |
| 86 | + |
| 87 | +**Component Name** |
| 88 | + |
| 89 | +Use the V3 convention for names, and make sure the generated |
| 90 | +function has the name of the enclosing module/file. |
| 91 | + |
| 92 | +**Fragment** |
| 93 | + |
| 94 | +```rescript |
| 95 | +<> comp1 comp2 comp3 </> |
| 96 | +
|
| 97 | +// is converted to |
| 98 | +
|
| 99 | +ReactDOMRe.createElement(ReasonReact.fragment, [comp1, comp2, comp3]) |
| 100 | +``` |
0 commit comments