-
Notifications
You must be signed in to change notification settings - Fork 406
/
08-nested-components.html
45 lines (35 loc) · 1.13 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
<!doctype html>
<title>08 Nested Components - React From Zero</title>
<script src="https://unpkg.com/[email protected]/umd/react.development.js"></script>
<script src="https://unpkg.com/[email protected]/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">
// 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>