Created
December 9, 2016 03:10
-
-
Save dengjonathan/a0474a284e0bde67dde6a515a1c650cf to your computer and use it in GitHub Desktop.
Counter React Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Counter.jsx | |
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
// PRESENTATIONAL COMPONENT: a simple stateless React View | |
const Button = ({onClick, label}) => ( | |
<button onClick={onClick}>{label}</button> | |
); | |
// REACT COMPONENT: a stateful React View (stored in this.state) | |
// Normally you'd want so other Model to store state (i.e. Redux) | |
class Counter extends React.Component { | |
constructor() { | |
super(); | |
this.state = { | |
value: 0 | |
} | |
} | |
// CONTROLLERS: update state based on user input | |
increment() { | |
this.setState({value: this.state.value + 1}); | |
} | |
decrement() { | |
this.setState({value: this.state.value - 1}); | |
} | |
render() { | |
return ( | |
<div className="counter"> | |
<Button onClick={this.increment.bind(this)} label={'+'}/> | |
<p id="count">{this.state.value}</p> | |
<Button onClick={this.decrement.bind(this)} label={'-'}/> | |
</div> | |
); | |
} | |
} | |
ReactDOM.render(<Counter />, document.getElementById('app')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment