-
-
Save vagusX/d5d2e7d56f17cafbf06bbcc3e9f88a7e to your computer and use it in GitHub Desktop.
Revisions
-
acdlite renamed this gist
Jun 6, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
acdlite revised this gist
Jun 6, 2015 . 2 changed files with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes.File renamed without changes. -
acdlite revised this gist
Jun 6, 2015 . 1 changed file with 7 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,10 @@ /** * Basic proof of concept. * - Hot reloadable * - Stateless stores * - Stores and action creators interoperable with Redux. */ import React, { Component } from 'react'; export default function dispatch(store, atom, action) { -
acdlite revised this gist
Jun 6, 2015 . 2 changed files with 2 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,5 @@ import React, { Component } from 'react'; export default function dispatch(store, atom, action) { return store(atom, action); } 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,6 @@ /** * Example usage * * Based on Redux's counter example * https://github.com/gaearon/redux/tree/master/examples/counter */ -
acdlite revised this gist
Jun 6, 2015 . 1 changed file with 81 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,81 @@ /** * Based on Redux's counter example * https://github.com/gaearon/redux/tree/master/examples/counter */ import React, { Component, PropTypes } from 'react'; import { Dispatcher, Injector } from '../'; const INCREMENT_COUNTER = 'INCREMENT_COUNTER'; const DECREMENT_COUNTER = 'DECREMENT_COUNTER'; function counterStore(counter = 0, action) { switch (action.type) { case INCREMENT_COUNTER: return counter + 1; case DECREMENT_COUNTER: return counter - 10; default: return counter; } } function increment() { return { type: INCREMENT_COUNTER }; } function decrement() { return { type: DECREMENT_COUNTER }; } export default class CounterApp { render() { return ( <Dispatcher // Instead of specifying an object of keys mapped to stores, just use a // higher-order store! store={(state = {}, action) => ({ counter: counterStore(state.counter, action) })} > {() => ( <Injector actions={{ increment, decrement }}> {({ actions, atom }) => ( <Counter increment={actions.increment} decrement={actions.decrement} counter={atom.counter} /> )} </Injector> )} </Dispatcher> ); } } class Counter { static propTypes = { increment: PropTypes.func.isRequired, decrement: PropTypes.func.isRequired, counter: PropTypes.number.isRequired }; render() { const { increment, decrement, counter } = this.props; return ( <p> Clicked: {counter} times {' '} <button onClick={increment}>+</button> {' '} <button onClick={decrement}>-</button> </p> ); } } -
acdlite renamed this gist
Jun 6, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
acdlite created this gist
Jun 6, 2015 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,74 @@ import React, { Component } from 'react'; export default function dispatch(store, atom, action) { return store(atom, action); } export class Dispatcher extends Component { static propTypes = { store: React.PropTypes.func.isRequired }; static childContextTypes = { dispatch: React.PropTypes.func, atom: React.PropTypes.any }; getChildContext() { return { atom: this.state.atom, dispatch: this.dispatch.bind(this) }; } constructor(props, context) { super(props, context); this.state = { atom: dispatch(props.store, undefined, {}) }; } dispatch(payload) { this.setState(prevState => ({ atom: dispatch(this.props.store, prevState.atom, payload) })); } render() { return typeof this.props.children === 'function' ? this.props.children(this.state.atom) : this.props.children; } } export class Injector extends Component { static contextTypes = { dispatch: React.PropTypes.func.isRequired, atom: React.PropTypes.any }; static propTypes = { actions: React.PropTypes.object }; performAction(actionCreator, ...args) { const { dispatch } = this.context; const payload = actionCreator(...args); return typeof payload === 'function' ? payload(dispatch) : dispatch(payload); }; render() { const { dispatch, atom } = this.context; const { actions: _actions } = this.props; const actions = Object.keys(_actions).reduce((result, key) => { result[key] = this.performAction.bind(this, _actions[key]); return result; }, {}); return this.props.children({ actions, atom }); } }