React reactive swiss army knife
This file contains 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 from "react" | |
import Bacon from "baconjs" | |
import {render} from "react-dom" | |
function Counter(initial) { | |
const incBus = new Bacon.Bus() | |
const value = incBus.map(1).scan(initial, (state, inc) => state + inc) | |
value.inc = () => incBus.push() | |
return value | |
} | |
const a = Counter(10) | |
const b = Counter(5) | |
const c = Bacon.combineWith(a, b, (a, b) => a + b) | |
const renderCounter = counter => Bacon.combineTemplate( | |
<div> | |
Value: {counter} | |
<button onClick={counter.inc}>+</button> | |
</div> | |
) | |
const app = Bacon.combineTemplate( | |
<div> | |
<h1>Counterz!</h1> | |
Counter A: {renderCounter(a)} | |
Counter B: {renderCounter(b)} | |
<hr /> | |
{a} + {b} = {c} | |
</div> | |
) | |
app.onValue(appEl => render(appEl, document.getElementById("todoapp"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment