Skip to content

Commit 1aae5d1

Browse files
authored
Add documentation for rendering components to DOM
Because I found this confusing when starting out, especially if I needed to do an explicit import.
1 parent a6546e2 commit 1aae5d1

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

pages/docs/react/latest/components-and-props.mdx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,38 @@ We've created a `Greeting.res` file that contains a `make` function that doesn't
5353

5454
You can also see in the the JS output that the function we created was directly translated into the pure JS version of a ReactJS component. Note how a `<div>` transforms into a `React.createElement("div",...)` call in JavaScript.
5555

56+
## Rendering Components
57+
58+
You can render a component by using the JSX syntax for that component. For e.g. to render Greeting, you would use something like this:
59+
60+
61+
<CodeTab labels={["ReScript", "JS Output"]}>
62+
63+
```res
64+
// src/App.res
65+
switch ReactDOM.querySelector("#root") {
66+
| Some(root) => ReactDOM.render(<Greeting />, root)
67+
| None => () // do nothing
68+
}
69+
```
70+
```js
71+
import * as React from "react";
72+
import * as ReactDom from "react-dom";
73+
import * as Greeting$RescriptReact from "./Greeting.bs.js";
74+
75+
var root = document.querySelector("#root");
76+
77+
if (!(root == null)) {
78+
ReactDom.render(React.createElement(Greeting$RescriptReact.make, {}), root);
79+
}
80+
```
81+
82+
Note that Rescript automatically takes care of importing the Greeting component in the generated JavaScript, you do not need to import anything manually. Components can also be composed within other components, just like regular React & JSX.
83+
84+
</CodeTab>
85+
86+
87+
5688
## Defining Props
5789

5890
In ReactJS, props are usually described as a single `props` object. In ReScript, we use [labeled arguments](/docs/manual/latest/function#labeled-arguments) to define our props parameters instead. Here's an example:

0 commit comments

Comments
 (0)