Last active
December 14, 2018 10:29
-
-
Save maumchaves/562d0af1532d0850620a7083a20efa7d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
* By Dan Abramov, @dan_abramov | |
* https://mobile.twitter.com/dan_abramov/status/1018945865745084416 | |
*/ | |
import React from "react"; | |
import ReactDOM from "react-dom"; | |
import "./styles.css"; | |
const actions = { | |
init() { | |
return { value: 0 }; | |
}, | |
increment(state) { | |
return { value: state.value + 1 }; | |
}, | |
decrement(state) { | |
return { value: state.value - 1 }; | |
} | |
}; | |
class App extends React.Component { | |
state = actions.init(); | |
handleIncrement = () => { | |
this.setState(actions.increment); | |
}; | |
handleDecrement = () => { | |
this.setState(actions.decrement); | |
}; | |
render() { | |
return ( | |
<div> | |
<h2>{this.state.value}</h2> | |
<button onClick={this.handleIncrement}>+</button> | |
<button onClick={this.handleDecrement}>-</button> | |
</div> | |
); | |
} | |
} | |
const rootElement = document.getElementById("root"); | |
ReactDOM.render(<App />, rootElement); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment