forked from kay-is/react-from-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
03-nested-elements.html
58 lines (43 loc) · 1.37 KB
/
03-nested-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
<!doctype html>
<title>03 Nested Elements - React From Zero</title>
<script src="https://unpkg.com/[email protected]/dist/react.js"></script>
<script src="https://unpkg.com/[email protected]/dist/react-dom.js"></script>
<script src="https://unpkg.com/[email protected]/browser.min.js"></script>
<div id="app"></div>
<script type="text/babel">
// Elements can be nested, which will result in nested React.createElement calls
// As you can imagine, writing withs without JSX would be pretty tedious
var reactElement =
<div className='abc'>
<h1>Hello</h1>
<h2>world</h2>
</div>
// they can also, like mentioned in lesson 2, contain JavaScript in {}
var myClass = 'abc'
function myText() { return 'world' }
// JavaScript insertion has the same syntax in attributes as in normal text or elements
reactElement =
<div className={myClass}>
<h1>Hello {10 * 10}</h1>
<h2>{myText()}</h2>
</div>
// this JavaScript can contain elements too
var nestedElement = <h2>world</h2>
reactElement =
<div>
<h1>Hello</h1>
{nestedElement}
</div>
// it is also possible to "spread" an object as properties
var properties = {
className: 'abc',
onClick: function() { alert('click') },
}
reactElement =
<div {...properties}>
<h1>Hello</h1>
<h2>world</h2>
</div>
var renderTarget = document.getElementById('app')
ReactDOM.render(reactElement, renderTarget)
</script>