Skip to content

Commit 757291e

Browse files
committed
add create-react-node.md
1 parent 35dcbdf commit 757291e

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Creating React Nodes
2+
3+
Most developers using React will prefer JSX and use it to create React nodes. Otherwise, in this chapter we are going to test how React nodes can be created without JSX, using only JavaScript. The next chapter we will discuss creating React nodes using JSX.
4+
5+
Creating React nodes using JavaScript is as simple as calling the `React.createElement(type, props, children)` function and passing it a set of arguments defining an actual DOM node (e.g. type = an html element e.g. `<option>` or custom element e.g. `<my-option>`).
6+
7+
The `React.createElement()` arguments:
8+
9+
* `type (string | createReactClass())`: can be a string which represents an HTML element (or custom HTML element) or React component instance (i.e., an instance of createReactClass()).
10+
* `props (null | object)`: can be `null` or an object containing attributes/props and values.
11+
* `children (null | string | createReactClass() | React.createElement())`: children can be null, a string that gets turned into a text node, an instance of createReactClass() or React.createElement()
12+
13+
Below I use the `React.createElement()` function to create a virtual DOM representation of a `<option>` element node containing a text node of one (React text) and with value 1.
14+
15+
```
16+
var reactNodeOption = React.createElement('option', { value: 1 }, "one");
17+
```
18+
19+
Notice that the first argument defines the HTML element I want to represent. The second argument defines the attributes/props on the `<option>`. And the third argument defines what the node inside of the element will be. In this case, I am placing a child text node (i.e. 'one') inside the `<option>`. The last argument that becomes a child of the node being created can be:
20+
21+
* A React text node
22+
* A React element node, or
23+
* A React component instance
24+
25+
So far I've done is creating a React element node containing a React text node that I have stored into the variable reactNodeOption. To create a virtual DOM we have to actually render the React element node to a real DOM. To do this we use the `ReactDOM.render()` function:
26+
27+
```
28+
ReactDOM.render(reactNodeOption, document.getElementById('app'));
29+
```
30+
31+
The above code, invokes the following:
32+
33+
* Create a virtual DOM constructed from the React element node passed in (reactNodeOption)
34+
* Use the virtual DOM to re-construct a real DOM branch
35+
* Insert the real DOM branch (i.e. `<option value="1">one</option>`) into the DOM as a child node of `<div id="app"></div>`.
36+
37+
The HTML DOM changes from this:
38+
39+
```
40+
<div id="app"></div>
41+
```
42+
43+
To
44+
45+
```
46+
<div id="app">
47+
// Note that React automatically added the react data-reactid attribute
48+
<option value="1" data-reactid=".0">one</option>
49+
</div>
50+
```
51+
52+
This was a simplistic example.
53+
54+
Let use `React.createElement()` to create a complex structure. For example, below I'm using `React.createElement()` to create a bunch of React nodes representing an HTML select (i.e. `<select>`).
55+
56+
```
57+
// Create React element <option>
58+
var rElmOption1 = React.createElement('option', { value: 1 }, "one"),
59+
rElmOption2 = React.createElement('option', { value: 2 }, "two"),
60+
rElmOption3 = React.createElement('option', { value: 3 }, "three"),
61+
rElmOption4 = React.createElement('option', { value: 4 }, "four");
62+
63+
// Create <select> React element and add child
64+
// React <option> elements to it
65+
var reactElementUl = React.createElement('select', {className: 'mySelect'}, rElmOption1, rElmOption2, rElmOption3, rElmOption4);
66+
```
67+
68+
When the above code is rendered to the DOM the result HTML will look like:
69+
70+
```
71+
<select class="mySelect" data-reactid=".0">
72+
<option value="1" data-reactid=".0.0">one</li>
73+
<option value="2" data-reactid=".0.1">two</li>
74+
<option value="3" data-reactid=".0.2">three</li>
75+
<option value="4" data-reactid=".0.3">four</li>
76+
</ul>
77+
```
78+
79+
You can try this yourself using the CodePen below:
80+
81+
<p data-height="265" data-theme-id="dark" data-slug-hash="XqBGaz" data-default-tab="js,result" data-user="Bunlong" data-embed-version="2" data-pen-title="XqBGaz" class="codepen">See the Pen <a href="https://codepen.io/Bunlong/pen/XqBGaz/">XqBGaz</a> by Bunlong (<a href="https://codepen.io/Bunlong">@Bunlong</a>) on <a href="https://codepen.io">CodePen</a>.</p>
82+
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
83+
84+
We can realize that React nodes are just JavaScript objects in a tree that represent real DOM nodes inside of a virtual DOM tree. The virtual DOM is then used to construct an actual DOM branch in an HTML page.
85+

0 commit comments

Comments
 (0)