mirror of
https://github.com/kay-is/react-from-zero.git
synced 2026-04-24 03:00:06 -04:00
58 lines
1.4 KiB
HTML
58 lines
1.4 KiB
HTML
<!doctype html>
|
|
|
|
<title>03 Nested Elements - 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">
|
|
|
|
// Elements can be nested, which will result in nested React.createElement calls
|
|
// As you can imagine, writing withs without JSX would be pretty tedious
|
|
var reactElement =
|
|
<div className='abc'>
|
|
<h1>Hello</h1>
|
|
<h2>world</h2>
|
|
</div>
|
|
|
|
// they can also, like mentioned in lesson 2, contain JavaScript in {}
|
|
var myClass = 'abc'
|
|
|
|
function myText() { return 'world' }
|
|
|
|
// JavaScript insertion has the same syntax in attributes as in normal text or elements
|
|
reactElement =
|
|
<div className={myClass}>
|
|
<h1>Hello {10 * 10}</h1>
|
|
<h2>{myText()}</h2>
|
|
</div>
|
|
|
|
// this JavaScript can contain elements too
|
|
var nestedElement = <h2>world</h2>
|
|
|
|
reactElement =
|
|
<div>
|
|
<h1>Hello</h1>
|
|
{nestedElement}
|
|
</div>
|
|
|
|
// it is also possible to "spread" an object as properties
|
|
var properties = {
|
|
className: 'abc',
|
|
onClick: function() { alert('click') },
|
|
}
|
|
|
|
reactElement =
|
|
<div {...properties}>
|
|
<h1>Hello</h1>
|
|
<h2>world</h2>
|
|
</div>
|
|
|
|
var renderTarget = document.getElementById('app')
|
|
|
|
ReactDOM.render(reactElement, renderTarget)
|
|
|
|
</script> |