Cleaner React with Class Fields.
Now, write class methods that are automatically bound to the class instance without any overhead syntax.
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
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
workDone: 'Work done!'
};
}
doWork = () => {
alert(this.state.workDone);
}
render() {
return (
<button onClick={this.doWork}>Do Work</button>
);
}
}
ReactDOM.render(
<App />,
document.getElementById('container')
);