forked from kay-is/react-from-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
05-properties.html
47 lines (33 loc) · 1.19 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
<!doctype html>
<title>05 Properties - React From Zero</title>
<script src="https://unpkg.com/react@15.4.2/dist/react.js"></script>
<script src="https://unpkg.com/react-dom@15.4.2/dist/react-dom.js"></script>
<script src="https://unpkg.com/babel-core@5.8.38/browser.min.js"></script>
<div id="app"></div>
<script type="text/babel">
// Components, like elements, can use properties, too
function MyComponent(props) {
return (
<div className={props.className}>
<h1>Hello</h1>
<h2>{props.customData}</h2>
</div>
)
}
var reactElement = <MyComponent className='abc' customData='world'/>
// Which also works with an object and the spread (...) operator
var props = {
className: 'abc',
customData: 'world',
}
reactElement = <MyComponent {...props}/>
// This allows components with dynamic content
var planets = ['earth', 'mars', 'venus']
// If an array is used as "child node" each child needs a unique key-property
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>