forked from kay-is/react-from-zero
-
Notifications
You must be signed in to change notification settings - Fork 4
/
03-nested-elements.html
57 lines (44 loc) · 1.41 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
<!doctype html>
<title>03 Elementos Anidados - React Drom Cero</title>
<script src="https://unpkg.com/react@16.4.0/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16.4.0/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<div id="app"></div>
<script type="text/babel">
// Los elementos pueden ser anidados, lo que se traducirá en llamadas anidads de
// React.createElement
// Como puedes imaginar, escribir esto sin JSX sería muy repetitivo e ilegible.
var reactElement =
<div className="abc">
<h1>Hello</h1>
<h2>world</h2>
</div>
// Recuerda que en JSX, escribir expresiones Javascript dentro de {}
var myClass = "abc"
function myText() { return "world" }
// Javascript tiene la misma sintáxis, incluso si está dentor de JSX
reactElement =
<div className={myClass}>
<h1>Hello {10 * 10}</h1>
<h2>{myText()}</h2>
</div>
// este JavaScript puede también contener otros elementos
var nestedElement = <h2>world</h2>
reactElement =
<div>
<h1>Hello</h1>
{nestedElement}
</div>
// es posible desestructurar un objeto como propiedades
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>