mirror of
https://github.com/kay-is/react-from-zero.git
synced 2026-04-24 03:00:06 -04:00
46 lines
1.4 KiB
HTML
46 lines
1.4 KiB
HTML
<!doctype html>
|
|
|
|
<title>02 JSX - React From Zero</title>
|
|
|
|
<script src="https://unpkg.com/react@16.4.0/umd/react.development.js"></script>
|
|
<script src="https://unpkg.com/react-dom@16.4.0/umd/react-dom.development.js"></script>
|
|
|
|
<script src="https://unpkg.com/@babel/standalone/babel.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> |