Skip to content

Instantly share code, notes, and snippets.

@eanplatter
Created August 30, 2016 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eanplatter/1783fe168b93af18974311c5258f2cf8 to your computer and use it in GitHub Desktop.
Save eanplatter/1783fe168b93af18974311c5258f2cf8 to your computer and use it in GitHub Desktop.
An attempt at doing an Elm architecture style React component.
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