forked from kay-is/react-from-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
08-nested-components.html
46 lines (34 loc) · 1.1 KB
/
08-nested-components.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
<!doctype html>
<title>08 Nested Components - 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">
// Components, like elements, can be nested
// for this, the children property is used inside the component
// This component just wraps its children in an <li> element
function Item(props) {
return <li>{props.children}</li>
}
// This component wraps its children into an <ul> element
function List(props) {
return <ul>{props.children}</ul>
}
// If the <List> is created without children it gets a default child
List.defaultProps = {
children: <Item>Empty</Item>
}
// now we render two <List>s, without and with Items
var reactElement =
<div>
<List/>
<List>
<Item>First</Item>
<Item>Second</Item>
<Item>Third</Item>
</List>
</div>
var renderTarget = document.getElementById('app')
ReactDOM.render(reactElement, renderTarget)
</script>