Skip to content

Instantly share code, notes, and snippets.

@maumchaves
Last active December 14, 2018 10:29
Show Gist options
  • Save maumchaves/562d0af1532d0850620a7083a20efa7d to your computer and use it in GitHub Desktop.
Save maumchaves/562d0af1532d0850620a7083a20efa7d to your computer and use it in GitHub Desktop.
/*
* 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