forked from kay-is/react-from-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
00-object-elements.html
75 lines (61 loc) · 2.31 KB
/
00-object-elements.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<!doctype html>
<title>00 Object Elements - React From Zero</title>
<script src="https://unpkg.com/[email protected]/umd/react.development.js"></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script>
<div id="app">
<!--The render target of our React application-->
</div>
<script>
// React uses ES2015 Symbols to "tag" its element-objects.
// It uses a magic number as fallback on older browsers.
var magicValue = (Symbol && Symbol.for("react.element")) || 0xeac7;
// React uses virtual DOM elements, which become real DOM elements
// on a render. A virtual DOM element can be defined as a simple
// object literal. Normally you would use the React.createElement()
// to create an element. This is what the return value of a
// React.createElement() call could look like.
var reactElement = {
// This special property will be checked by React to ensure this
// object
// is a React element and not just some user data
// React.createElement() sets it for you
$$typeof: magicValue,
// This will also be checked by React. We will be talking about
// references later, but if you're not using them, this has to be
// set to null and not undefined
ref: null,
// This defines the HTML-tag
type: "h1",
// This defines the properties that get passed down to the element
props: {
// In this example there is just a single text node as child
children: "Hello, world!",
// a CSS class
className: "abc",
// styles can be passed as object literals
// React uses camelCase instead of dashed-case (like CSS/D3 do)
style: {
textAlign: "center"
},
// event handlers can be added as properties, too
// React uses synthetic events to try to normalize browser
// behavior
onClick: function (notYourRegularEvent) {
alert("click");
}
}
};
// another element that doesn"t have much configuration
var anotherElement = {
$$typeof: magicValue,
ref: null,
type: "p",
props: {
children: "A nice text paragraph."
}
};
// React needs a DOM element as render target
var renderTarget = document.getElementById("app");
// ReactDOM is responsible for inserting the element into the DOM
ReactDOM.render(reactElement, renderTarget);
</script>