Created
August 30, 2016 15:07
-
-
Save eanplatter/1783fe168b93af18974311c5258f2cf8 to your computer and use it in GitHub Desktop.
An attempt at doing an Elm architecture style React component.
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
import React, { Component } from 'react' | |
import { render } from 'react-dom' | |
const INCREMENT = 'increment' | |
const DECREMENT = 'decrement' | |
class App extends Component { | |
// model | |
state = { | |
counter: 0 | |
} | |
// update | |
update(action) { | |
switch(action.type) { | |
case INCREMENT: | |
this.setState({ | |
counter: this.state.counter + 1, | |
}) | |
return | |
case DECREMENT: | |
this.setState({ | |
counter: this.state.counter - 1, | |
}) | |
return | |
default: | |
return | |
} | |
} | |
// view | |
render() { | |
return ( | |
<div> | |
<h1> | |
{`The count: ${this.state.counter}`} | |
</h1> | |
<div> | |
<button onClick={() => this.update({type: INCREMENT})}> | |
Increment + | |
</button> | |
<button onClick={() => this.update({type: DECREMENT})}> | |
Decrement - | |
</button> | |
</div> | |
</div> | |
) | |
} | |
} | |
render(<App />, document.getElementById('root')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment