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/react/latest/beyond-jsx.mdx
+60-58
Original file line number
Diff line number
Diff line change
@@ -12,9 +12,9 @@ JSX is a syntax sugar that allows us to use React components in an HTML like man
12
12
13
13
</Intro>
14
14
15
-
**Note:** This section requires knowledge about the low level apis for [creating elements](./elements-and-jsx#creating-elements-from-component-functions), such as `React.createElement` or `ReactDOMRe.createDOMElementVariadic`.
15
+
**Note:** This section requires knowledge about the low level apis for [creating elements](./elements-and-jsx#creating-elements-from-component-functions), such as `React.createElement` or `ReactDOM.createDOMElementVariadic`.
16
16
17
-
> **Note:** This page assumes your `bsconfig.json` to be set to `"reason": { "react-jsx": 3 }` to apply the right JSX transformations.
17
+
> **Note:** This page assumes your `bsconfig.json` to be set to `"jsx": { "version": 4 }` to apply the right JSX transformations.
18
18
19
19
## Component Types
20
20
@@ -24,22 +24,19 @@ Here are some examples on how to define your own component types (often useful w
type props = {padding: string, children: React.element}
33
+
type containerComp = React.component<props>
37
34
```
38
35
The types above are pretty low level (basically the JS representation of a React component), but since ReScript React has its own ways of defining React components in a more language specific way, let's have a closer look on the anatomy of such a construct.
39
36
40
37
## JSX Component Interface
41
38
42
-
A ReScript React component needs to be a (sub-)module with a `make` and `makeProps` function to be usable in JSX. To make things easier, we provide a `@react.component` decorator to create those functions for you:
39
+
A ReScript React component needs to be a (sub-)module with a `make`function and `props` type to be usable in JSX. To make things easier, we provide a `@react.component` decorator to create those functions for you:
As shown in the expanded output above, our decorator desugars the function passed to `React.forwardRef` in the same manner as a typical component `make` function. It also creates a `makeProps` function with a `ref`prop, so we can use it in our JSX call (`<FancyInput ref=.../>`).
109
+
As shown in the expanded output above, our decorator desugars the function passed to `React.forwardRef` in the same manner as a typical component `make` function. It also creates a `props` type with an optional `ref`field, so we can use it in our JSX call (`<FancyInput ref=.../>`).
127
110
128
111
So now that we know how the ReScript React component transformation works, let's have a look on how ReScript transforms our JSX constructs.
129
112
130
113
## JSX Under the Hood
131
114
132
115
Whenever we are using JSX with a custom component ("capitalized JSX"), we are actually using `React.createElement` to create a new element. Here is an example of a React component without children:
As you can see, it uses `Friend.make`and `Friend.makeProps`to call the `React.createElement` API. In case you are providing children, it will use `React.createElementVariadic` instead (which is just a different binding for `React.createElement`):
135
+
As you can see, it uses `Friend.make` to call the `React.createElement` API. In case you are providing children, it will use `React.createElementVariadic` instead (which is just a different binding for `React.createElement`):
0 commit comments