You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pages/docs/manual/latest/jsx.mdx
+66-89Lines changed: 66 additions & 89 deletions
Original file line number
Diff line number
Diff line change
@@ -10,20 +10,19 @@ Would you like some HTML syntax in your ReScript? If not, quickly skip over this
10
10
11
11
ReScript supports the JSX syntax, with some slight differences compared to the one in [ReactJS](https://facebook.github.io/react/docs/introducing-jsx.html). ReScript JSX isn't tied to ReactJS; they translate to normal function calls:
12
12
13
-
**Note** for [ReasonReact](https://reasonml.github.io/reason-react/en/) readers: this isn't what ReasonReact turns JSX into, in the end. See Usage section for more info.
13
+
**Note** for [ReScriptReact](https://rescript-lang.org/docs/react/latest/introduction) readers: this isn't what ReScriptReact turns JSX into, in the end. See Usage section for more info.
This is the syntax for passing a list of two items, `child1` and `child2`, to the children position. It desugars to a list containing `child1` and `child2`:
114
+
This is the syntax for passing a list of two items, `child1` and `child2`, to the children position. It transforms to a list containing `child1` and `child2`:
**Note** again that this isn't the transform for ReasonReact; ReasonReact turns the final list into an array. But the idea still applies.
127
+
**Note** again that this isn't the transform for ReScriptReact; ReScriptReact turns the final list into an array. But the idea still applies.
128
+
129
+
So naturally, `<MyComponent> myChild </MyComponent>` is transformed to `MyComponent.createElement(~children=list{myChild}, ())`. I.e. whatever you do, the arguments passed to the children position will be wrapped in a list.
130
130
131
-
So naturally, `<MyComponent> myChild </MyComponent>` desugars to `@JSX MyComponent.createElement(~children=list{myChild}, ())`. I.e. whatever you do, the arguments passed to the children position will be wrapped in a list. What if you don't want that? **What if you want to directly pass `myChild` without an extra wrapping**?
131
+
## Usage
132
132
133
-
#### Children Spread
133
+
See [ReScriptReact Elements & JSX](https://rescript-lang.org/docs/react/latest/elements-and-jsx) for an example application of JSX, which transforms the above calls into a ReScriptReact-specific call.
This passes the value `myChild`_without_ wrapping it in a list (or array, in the case of ReasonReact). Aka, this desugars to:
162
+
## Departures From JS JSX
163
+
164
+
- Attributes and children don't mandate `{}`, but we show them anyway for ease of learning. Once you format your file, some of them go away and some turn into parentheses.
165
+
- The spread props is supported, but there are some restrictions.
This is extra useful in the cases where you are handled `myChild` that is already a list of things, and want to forward that without wrapping it an extra time (which would be a type error) \*. It also allows you to pass arbitrary data structures at `children` position (remember, JSX `children` is really just a totally normal prop):
See [ReasonReact JSX](https://reasonml.github.io/reason-react/docs/en/jsx) for an example application of JSX, which transforms the above calls into a ReasonReact-specific call.
188
-
189
-
Here's a JSX tag that shows most of the features.
196
+
The spread must be the first position followed by other props:
190
197
191
-
<CodeTablabels={["ReScript", "JS Output"]}>
198
+
<CodeTablabels={["ReScript"]}>
192
199
193
200
```res
194
-
<MyComponent
195
-
booleanAttribute={true}
196
-
stringAttribute="string"
197
-
intAttribute=1
198
-
forcedOptional=?{Some("hello")}
199
-
onClick={handleClick}>
200
-
<div> {React.string("hello")} </div>
201
-
</MyComponent>
202
-
```
203
-
```js
204
-
React.createElement(
205
-
MyComponent.make,
206
-
MyComponent.makeProps(
207
-
true,
208
-
"string",
209
-
1,
210
-
"hello",
211
-
handleClick,
212
-
React.createElement("div", undefined, "hello"),
213
-
undefined
214
-
)
215
-
);
201
+
<NotAllowed a="a" {...props} />
216
202
```
217
203
218
204
</CodeTab>
219
205
220
-
## Departures From JS JSX
221
-
222
-
- Attributes and children don't mandate `{}`, but we show them anyway for ease of learning. Once you format your file, some of them go away and some turn into parentheses.
223
-
- There is no support for JSX prop spread: `<Comp {...props} />`. Though somewhat related, we do have children spread, described above: `<Comp> ...children </Comp>`.
224
-
- Punning!
225
-
226
206
### Punning
227
207
228
208
"Punning" refers to the syntax shorthand for when a label and a value are the same. For example, in JavaScript, instead of doing `return {name: name}`, you can do `return {name}`.
229
209
230
-
Reason JSX supports punning. `<input checked />` is just a shorthand for `<input checked=checked />`. The formatter will help you format to the punned syntax whenever possible. This is convenient in the cases where there are lots of props to pass down:
210
+
JSX supports punning. `<input checked />` is just a shorthand for `<input checked=checked />`. The formatter will help you format to the punned syntax whenever possible. This is convenient in the cases where there are lots of props to pass down:
Consequently, a Reason JSX component can cram in a few more props before reaching for extra libraries solutions that avoids props passing.
227
+
Consequently, a JSX component can cram in a few more props before reaching for extra libraries solutions that avoids props passing.
247
228
248
229
**Note** that this is a departure from ReactJS JSX, which does **not** have punning. ReactJS' `<input checked />` desugars to `<input checked=true />`, in order to conform to DOM's idioms and for backward compatibility.
249
230
250
231
## Tip & Tricks
251
232
252
-
For library authors wanting to take advantage of the JSX: the `@JSX` attribute above is a hook for potential ppx macros to spot a function wanting to format as JSX. Once you spot the function, you can turn it into any other expression.
233
+
For library authors wanting to take advantage of the JSX: the `@JSX` attribute is a hook for potential ppx macros to spot a function wanting to format as JSX. Once you spot the function, you can turn it into any other expression.
253
234
254
-
This way, everyone gets to benefit the JSX syntax without needing to opt into a specific library using it, e.g. ReasonReact.
235
+
This way, everyone gets to benefit the JSX syntax without needing to opt into a specific library using it, e.g. ReScriptReact.
255
236
256
237
JSX calls supports the features of [labeled arguments](function.md#labeled-arguments): optional, explicitly passed optional and optional with default.
257
-
258
-
## Design Decisions
259
-
260
-
\* You might wonder why you never needed such children spread in ReactJS; ReactJS uses some special runtime logic + special syntax transforms + variadic argument detection & marking to avoid most of these cases ([see here](https://github.com/facebook/react/blob/9b36df86c6ccecb73ca44899386e6a72a83ad445/packages/react/src/ReactElement.js#L207)). Such dynamic usage complexifies the type system detection _quite a bit_. Since we control the whole syntax and ReasonReact, we decided to introduce children spread to disambiguate between the case of wrapping vs not wrapping, without compile-time & runtime cost!
0 commit comments