How to write JSX and convert it to runnable React using Babel-Standalone in the browser.
ReactDOM.render(
<h1>Hello, React!</h1>,
document.getElementById('root')
);
ReactDOM.render(React.createElement(
'h1',
null,
'Hello, React!'
), document.getElementById('root'));
<div id="root">
<h1>Hello, React!</h1>
</div>
Building a greeter
const user = {
firstName: 'Ada',
lastName: 'Lovelace',
nickName: 'The queen of numbers'
};
function formatName(user) {
return user.firstName + ' ' + user.lastName;
}
const element = (
<div>
<h1>Welcome to React, {formatName(user)}!</h1>
<h2>{user.nickName}</h2>
</div>
);
ReactDOM.render(
element,
document.getElementById('root')
);
const user = {
firstName: 'Ada',
lastName: 'Lovelace',
nickName: 'The queen of numbers'
};
function formatName(user) {
return user.firstName + ' ' + user.lastName;
}
const element = (
<div>
<h1>Welcome to React, {formatName(user)}!</h1>
<h2>{user.nickName}</h2>
</div>
);
ReactDOM.render(
element,
document.getElementById('root')
);
See the Pen Embedding with JSX by Joshua Burke (@Dangeranger) on CodePen.
<script src="https://unpkg.com/babel-standalone@6/babel.min.js">
<script type="text/babel">
console.log( <h1>yo</h1> );
</script>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script src="https://unpkg.com/react/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom/umd/react-dom.development.js"></script>
</head>
<body>
<div id="output"></div>
<!-- Load Babel and React -->
<!-- Your script here -->
<script type="text/babel">
ReactDOM.render(
<h1>Hello, again React!</h1>,
document.getElementById('output')
);
</script>
</body>
</html>
See the Pen Single File React by Joshua Burke (@Dangeranger) on CodePen.