React Base Fiddle (JSX)
Starting point for creating JSFiddles with React.
HTML
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div id="container">
<!-- This element's contents will be replaced with your component. -->
</div>
Babel + JSX
const animals = ["Cat", "Dog", "Rabbit"];
class RandomAnimal extends React.Component {
constructor() {
super();
this.state = { animal: null };
this.setRandomAnimal = this.setRandomAnimal.bind(this);
}
setRandomAnimal() {
const animal = animals[Math.floor(Math.random() * 3)];
this.setState({ animal });
}
render() {
let renderAnimal;
if (this.state.animal) {
renderAnimal = <h1>Hello, {this.state.animal}</h1>
}
return (
<React.Fragment>
<button onClick={this.setRandomAnimal}>Set Random Animal</button>
{ renderAnimal }
</React.Fragment>
)
}
}
ReactDOM.render(
<RandomAnimal />,
document.getElementById('container')
);