Files
react-from-zero/2-jsx.html
2016-04-26 21:16:24 +02:00

36 lines
1.4 KiB
HTML

<!doctype html>
<title>2 JSX - React From Zero</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.1/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.1/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/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 in an app
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>