Example React Button Component w/ onClick

// Example React.js Component var ButtonComponent = React.createClass({ getInitialState: function() { return { numClicks: 0 } } click: function() { this.setState(numClicks: this.state.numClicks + 1); }, render: function() { return ( <div> <button onClick={this.click.bind(this)}>Click Me</button> {this.state.numClicks} </div> ); } });
This component will increment the numClicks state variable every time you click the button. The onClick event on the button is `this.click.bind(this)` so that the state of the function is bound to the React class and not the event.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.