forked from kay-is/react-from-zero
-
Notifications
You must be signed in to change notification settings - Fork 13
/
05-properties.html
51 lines (39 loc) · 1.55 KB
/
05-properties.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
<!doctype html>
<title>05 Propriedades - React do Zero</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">
// Componentes, como elementos, podem usar propriedades, também
function MyComponent(props) {
return (
<div className={props.className}>
<h1>Hello</h1>
<h2>{props.customData}</h2>
</div>
)
}
var reactElement = <MyComponent className="abc" customData="world"/>
// O que também funciona com um objeto e com o operador de espalhamento
var props = {
className: "abc",
customData: "world",
}
reactElement = <MyComponent {...props}/>
// Isso permite componentes de conteúdo dinâmico
var planets = ["earth", "mars", "venus"]
// Se um array é usado como um nó filho, cada filho precisa de uma propriedade-chave única.
// Essa chamada da função map vai retornar um vetor de componentes, como no exemplo:
+// [
+// <MyComponent className="myClass" customData={"earth"} key={0}/>,
+// <MyComponent className="myClass" customData={"mars"} key={1}/>,
+// <MyComponent className="myClass" customData={"venus"} key={2}/>
+// ]
var elements = planets.map(function(planet, index) {
return <MyComponent className="myClass" customData={planet} key={index}/>
})
reactElement = <div>{elements}</div>
var renderTarget = document.getElementById("app")
ReactDOM.render(reactElement, renderTarget)
</script>