React Base Fiddle (JSX)
Starting point for creating JSFiddles with React.
by Andrew Dunai
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
// 0. Увага: цей код викидатиме помилку!
class App extends React.Component {
constructor() {
super();
this.state = {counter: 0};
}
increase() {
// 2. ОЙ! А тут this - це не екземпляр App, а якась дічь! O___O
// І що ж робити?
this.setState({counter: this.state.counter + 1});
}
render() {
return <div>
Counter equals to {this.state.counter}
<input type="button" onClick={this.increase} value="Click me!" />
</div>;
// 1. Хмм, ніби все окей - this.increase існує, і this тут вказує на те, що треба: на наш об'єкт
}
}
ReactDOM.render(
<App />,
document.getElementById('container')
);