forked from kay-is/react-from-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
02-jsx.html
48 lines (37 loc) · 1.39 KB
/
02-jsx.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
48
<!doctype html>
<title>02 JSX - 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">
// Now we will use JSX, which needs to be converted to JavaScript.
// For this we will use Babel. Babel is normally used to convert ES2015 to ES5, but
// it also can convert JSX to ES5. Babels browser version uses text/babel script tags.
</script>
<div id='app'></div>
<script type="text/babel">
// JSX is the idiomatic way of creating elements.
// It's basically XHTML with {} for dynamic content
// also class has to be called className
var reactElement =
<h1
className='abc'
style={{textAlign: 'center'}}
onClick={function() { alert('click') }}>
Hello, world!
</h1>
// Is the same as
reactElement =
React.createElement(
'h1',
{className: 'abc', style: {textAlign: 'center'},
onClick: function() { alert('click') }},
'Hello, world!'
)
// JSX shines especially with simple elements that make up the majority
var anotherElement = <p>A nice text paragraph.</p>
// Is the same as
anotherElement = React.createElement('p', null, 'A nice text paragraph.')
// As we can see, everything else works as before
var renderTarget = document.getElementById('app')
ReactDOM.render(reactElement, renderTarget)
</script>