Created
July 17, 2018 19:59
-
-
Save orodio/04ae6af23f94cdf1897e9210a19acc6b to your computer and use it in GitHub Desktop.
simon_counter2.js
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
const foo1 = () => { this.rawr = "rawr" } | |
function foo2 () { this.rawr = "rawr" } | |
function moo2 () { this.rawr = "moo" } | |
function resetRawr () { this.rawr = "rawr" } | |
function setRawr (value) { this.rawr = value } | |
const rawrStuff = { moo2: moo2, resetRawr: resetRawr, setRawr: setRawr } | |
foo1() | |
function Foo () { | |
this.rawr = "rawr" | |
this.resetRawr = function () { this.rawr = "rawr"} | |
} | |
Foo.prototype.changeRawr = function (value) { this.rawr = value } | |
const foo = new Foo() | |
foo.changeRawr("omg") | |
Object.assign(foo.prototype, rawrStuff, { foo2 }) | |
foo.rawr // "omg" | |
foo.moo2() | |
class Foo extends React.Componet{ | |
constructor (props) { | |
super(props) | |
this.rawr = "rawr" | |
} | |
resetRawr = function () { this.rawr = "rawr" } | |
changeRawr(value) { this.rawr = value } | |
} | |
import { createStore } from 'fe-lib-store' | |
import { connect } from 'fe-hoc-connect' | |
import { compose } from 'fe-hoc-compose' | |
const initialState = { | |
counterOne: 0, | |
counterTwo: 0, | |
counterThree: 0, | |
} | |
const { store } = createStore('counters', {}, initialState) | |
const connect = (store, mapState) => Comp => { | |
return class extends React.Component { | |
componentDidMount () { | |
this.mount = true | |
this.unsub = store.subscribe(() => this.mount && this.forceUpdate()) | |
} | |
componentWillUnmount () { | |
this.mount = false | |
this.unsub() | |
} | |
render () { | |
return <Comp {...this.props} {...mapState(store.getState(), this.props)}/> | |
} | |
} | |
} | |
const Btn = ({ children, onClick }) => | |
<button onClick={onClick}> | |
{ children } | |
</button> | |
const Text = ({ children }) => | |
<strong>{ children }</strong> | |
const Counter = ({ count, onInc, onDec }) => | |
<div> | |
<Text>{count}</Text> | |
{ onDec && <Btn onClick={onDec}>-</Btn> } | |
{ onInc && <Btn onClick={onInc}>+</Btn> } | |
</div> | |
const ConnectedCounter = connect(store, (state, { id }) => ({ | |
count: state[id], | |
}))(Counter) | |
const Counters = ({ counters, total }) => | |
<div> | |
<Title total={total}>Count</Title> | |
{ counters.map(id => <ConnectedCounter id={id}/>) } | |
</div> | |
const ConnectedCounters = compose( | |
connect(otherStore, (state, props) => ({ | |
activeCounter: state.activeCounter, | |
})), | |
connect(store, (state, props) => ({ | |
counters: Object.keys(state), | |
total: Object.keys(state).map(id => state[id]).reduce((a, b) => a+b), | |
})) | |
)(Counters) | |
<ConnectedCounters/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment