Skip to content

Instantly share code, notes, and snippets.

@ryanflorence
Last active January 22, 2016 00:21
Show Gist options
  • Save ryanflorence/66ecffce9db1cae5e96f to your computer and use it in GitHub Desktop.
Save ryanflorence/66ecffce9db1cae5e96f to your computer and use it in GitHub Desktop.
// " In short, the closer the code is to a description of “what I want the program to do”, the better."
// http://staltz.com/why-react-redux-is-an-inferior-paradigm.html
////////////////////////////////////////////////////////////////////////////////
// Cycle
import {Observable} from 'rx';
import Cycle from '@cycle/core';
import {div, button, p, makeDOMDriver} from '@cycle/dom';
function main({DOM}) {
let action$ = Observable.merge(
DOM.select('.decrement').events('click').map(ev => -1),
DOM.select('.increment').events('click').map(ev => +1)
);
let count$ = action$.startWith(0).scan((x,y) => x+y);
return {
DOM: count$.map(count =>
div([
button('.decrement', 'Decrement'),
button('.increment', 'Increment'),
p('Counter: ' + count)
])
)
};
}
Cycle.run(main, {
DOM: makeDOMDriver('#main-container')
});
////////////////////////////////////////////////////////////////////////////////
// React
import React from 'react'
import { render } from 'react-dom'
const Main = React.createClass({
getInitialState() {
return { count: 0 }
},
increment() {
this.setState({ count: this.state.count + 1 })
},
decrement() {
this.setState({ count: this.state.count - 1 })
},
render() {
return (
<div>
<button onClick={this.increment}>Increment</button>
<button onClick={this.decrement}>Decrement</button>
<p>Counter: {this.state.count}</p>
</div>
)
}
})
render(<Main/>, document.getElementById('main'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment