mirror of
https://github.com/kay-is/react-from-zero.git
synced 2026-04-24 03:00:06 -04:00
46 lines
1.1 KiB
HTML
46 lines
1.1 KiB
HTML
<!doctype html>
|
|
|
|
<title>8 Nested Components - React From Zero</title>
|
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react.js"></script>
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react-dom.js"></script>
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/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>
|