Skip to content

Commit bd0865b

Browse files
committed
Update jsx docs for v12
1 parent 0c8a847 commit bd0865b

File tree

2 files changed

+111
-38
lines changed

2 files changed

+111
-38
lines changed

pages/docs/manual/v12.0.0/build-configuration.mdx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,24 @@ Note that the path resolution for the command (`node` in this case) is done so:
116116

117117
The command itself is called from inside `lib/bs`.
118118

119+
## jsx
120+
121+
Controls how the compiler emits JSX and which runtime (if any) it delegates to. A minimal configuration looks like this:
122+
123+
```json
124+
{
125+
"jsx": {
126+
"version": 4
127+
}
128+
}
129+
```
130+
131+
- `version`: JSX transform version. `4` enables the React 17+ transform and is the current and only supported option in v12.
132+
- `module`: Override the target module that receives JSX calls. Useful for [generic JSX transforms](./jsx#generic-jsx-transform-jsx-beyond-react-experimental); omit it when using the built-in React runtime.
133+
- `preserve`: When `true`, the compiler re-emits JSX syntax in the generated JavaScript so bundlers or other tooling can take over the transform later. The regenerated JSX might differ slightly from the original source but stays semantically equivalent. See [Preserve mode](./jsx#preserve-mode) for details.
134+
135+
All fields are optional; unspecified fields fall back to the defaults mentioned above. Combine them as needed for your project's JSX runtime.
136+
119137
## package-specs
120138

121139
Output to either CommonJS (the default) or JavaScript module. Example:

pages/docs/manual/v12.0.0/jsx.mdx

Lines changed: 93 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ ReScript supports the JSX syntax, with some slight differences compared to the o
2323
```
2424

2525
```js
26-
React.createElement(MyComponent, {
26+
import * as JsxRuntime from "react/jsx-runtime";
27+
28+
JsxRuntime.jsx(MyComponent, {
2729
name: "ReScript",
2830
});
2931
```
@@ -35,11 +37,13 @@ becomes
3537
<CodeTab labels={["ReScript", "JS Output"]}>
3638

3739
```res
38-
MyComponent.createElement(~name="ReScript", ~children=list{}, ())
40+
React.jsx(MyComponent.make, {name: {"ReScript"}})
3941
```
4042

4143
```js
42-
React.createElement(MyComponent, {
44+
import * as JsxRuntime from "react/jsx-runtime";
45+
46+
JsxRuntime.jsx(MyComponent, {
4347
name: "ReScript",
4448
});
4549
```
@@ -55,14 +59,12 @@ React.createElement(MyComponent, {
5559
```
5660

5761
```js
58-
React.createElement(
59-
"div",
60-
{
61-
onClick: handler,
62-
},
63-
child1,
64-
child2,
65-
);
62+
import * as JsxRuntime from "react/jsx-runtime";
63+
64+
JsxRuntime.jsxs("div", {
65+
children: [child1, child2],
66+
onClick: handler,
67+
});
6668
```
6769

6870
</CodeTab>
@@ -72,18 +74,16 @@ becomes
7274
<CodeTab labels={["ReScript", "JS Output"]}>
7375

7476
```res
75-
div(~onClick=handler, ~children=list{child1, child2}, ())
77+
ReactDOM.jsxs("div", {onClick: {handler}, children: React.array([child1, child2])})
7678
```
7779

7880
```js
79-
React.createElement(
80-
"div",
81-
{
82-
onClick: handler,
83-
},
84-
child1,
85-
child2,
86-
);
81+
import * as JsxRuntime from "react/jsx-runtime";
82+
83+
JsxRuntime.jsxs("div", {
84+
children: [child1, child2],
85+
onClick: handler,
86+
});
8787
```
8888

8989
</CodeTab>
@@ -97,7 +97,11 @@ React.createElement(
9797
```
9898

9999
```js
100-
React.createElement(React.Fragment, undefined, child1, child2);
100+
import * as JsxRuntime from "react/jsx-runtime";
101+
102+
JsxRuntime.jsxs(JsxRuntime.Fragment, {
103+
children: [child1, child2],
104+
});
101105
```
102106

103107
</CodeTab>
@@ -107,11 +111,15 @@ becomes
107111
<CodeTab labels={["ReScript", "JS Output"]}>
108112

109113
```res
110-
list{child1, child2}
114+
React.jsxs(React.jsxFragment, {children: React.array([child1, child2])})
111115
```
112116

113117
```js
114-
React.createElement(React.Fragment, undefined, child1, child2);
118+
import * as JsxRuntime from "react/jsx-runtime";
119+
120+
JsxRuntime.jsxs(JsxRuntime.Fragment, {
121+
children: [child1, child2],
122+
});
115123
```
116124

117125
</CodeTab>
@@ -125,7 +133,11 @@ React.createElement(React.Fragment, undefined, child1, child2);
125133
```
126134

127135
```js
128-
React.createElement(MyComponent, { children: null }, child1, child2);
136+
import * as JsxRuntime from "react/jsx-runtime";
137+
138+
JsxRuntime.jsxs(MyComponent, {
139+
children: [child1, child2],
140+
});
129141
```
130142

131143
</CodeTab>
@@ -135,23 +147,22 @@ This is the syntax for passing a list of two items, `child1` and `child2`, to th
135147
<CodeTab labels={["ReScript", "JS Output"]}>
136148

137149
```res
138-
MyComponent.createElement(~children=list{child1, child2}, ())
150+
React.jsxs(MyComponent.make, {children: React.array([child1, child2])})
139151
```
140152

141153
```js
142-
React.createElement(
143-
MyComponent.make,
144-
MyComponent.makeProps(null, undefined),
145-
child1,
146-
child2,
147-
);
154+
import * as JsxRuntime from "react/jsx-runtime";
155+
156+
JsxRuntime.jsxs(MyComponent, {
157+
children: [child1, child2],
158+
});
148159
```
149160

150161
</CodeTab>
151162

152163
**Note** again that this isn't the transform for ReScriptReact; ReScriptReact turns the final list into an array. But the idea still applies.
153164

154-
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.
165+
So naturally, `<MyComponent> myChild </MyComponent>` is transformed to `React.jsx(MyComponent.make, {children: myChild})`. I.e. whatever you do, the arguments passed to the children position will be wrapped in a list.
155166

156167
## Usage
157168

@@ -173,8 +184,12 @@ Here's a JSX tag that shows most of the features.
173184
```
174185

175186
```js
176-
React.createElement(MyComponent, {
177-
children: React.createElement("div", undefined, "hello"),
187+
import * as JsxRuntime from "react/jsx-runtime";
188+
189+
JsxRuntime.jsx(Playground$MyComponent, {
190+
children: JsxRuntime.jsx("div", {
191+
children: "hello",
192+
}),
178193
booleanAttribute: true,
179194
stringAttribute: "string",
180195
intAttribute: 1,
@@ -205,7 +220,9 @@ JSX props spread is supported now, but in a stricter way than in JS.
205220
```
206221

207222
```js
208-
React.createElement(Comp, {
223+
import * as JsxRuntime from "react/jsx-runtime";
224+
225+
JsxRuntime.jsx(Comp.make, {
209226
a: "a",
210227
b: "b",
211228
});
@@ -246,9 +263,11 @@ JSX supports punning. `<input checked />` is just a shorthand for `<input checke
246263
```
247264

248265
```js
249-
React.createElement(MyComponent, {
250-
isLoading: true,
266+
import * as JsxRuntime from "react/jsx-runtime";
267+
268+
JsxRuntime.jsx(MyComponent, {
251269
text: text,
270+
isLoading: true,
252271
onClick: onClick,
253272
});
254273
```
@@ -276,7 +295,9 @@ such props in your custom `JsxDOM.domProps` type ([see generic JSX transform](#g
276295
```
277296

278297
```js
279-
React.createElement("model-viewer", {
298+
import * as JsxRuntime from "react/jsx-runtime";
299+
300+
JsxRuntime.jsx("model-viewer", {
280301
"touch-actions": "pan-y",
281302
src: src,
282303
});
@@ -437,3 +458,37 @@ To enable this, you need to configure the `jsx` `module` in your `rescript.json`
437458
```
438459

439460
_value "Preact" is the name of the module that implements the generic JSX transform._
461+
462+
## Preserve mode
463+
464+
**Since 12.0**
465+
466+
JSX Preserve Mode keeps JSX syntax in the compiled JavaScript output instead of transforming it to `JsxRuntime.jsx` calls. This lets bundlers (ESBuild, SWC, Next.js) or React Server Components handle JSX transformation.
467+
468+
### Configuration
469+
470+
```json
471+
{
472+
"jsx": {
473+
"version": 4,
474+
"preserve": true
475+
}
476+
}
477+
```
478+
479+
<CodeTab labels={["ReScript", "JS Output"]}>
480+
481+
```res
482+
let c1 = <div className="foo"> {React.string("Hello")} </div>
483+
let c2 = <MyComponent text="hey" isLoading=true onClick />
484+
```
485+
486+
```js
487+
let c1 = <div className={"foo"}>{"Hello"}</div>;
488+
489+
let c2 = <MyComponent.make text={"hey"} isLoading={true} onClick={onClick} />;
490+
```
491+
492+
</CodeTab>
493+
494+
Note that the JSX output is functional but not always the most aesthetically pleasing.

0 commit comments

Comments
 (0)