Created
March 10, 2017 00:49
-
-
Save nelix/49e49d8046036e1bcffcfd6768a2945b to your computer and use it in GitHub Desktop.
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 characters
function appReducer(state={}, action) { | |
const {meta, ...rest} = action; | |
const {app, ...mrest} = meta; | |
if (!app) { | |
return state; | |
} | |
return { | |
...state, | |
[app]: appReducer(state[app], {...rest, meta: mrest}), | |
}; | |
} | |
export default class NamespaceProvider extends Component { | |
static childContextTypes = { store: React.PropTypes.object } | |
static contextTypes = { store: React.PropTypes.object } | |
getChildContext() { | |
return { | |
store: this.store, | |
}; | |
} | |
constructor(props, context) { | |
super(props, context); | |
const {store = props.store, app = props.app} = context; | |
this.store = { | |
...store, | |
getState: (...args) => | |
store.getState(...args)[app]; | |
dispatch: (action) => | |
store.dispatch({ | |
...action, | |
meta: { | |
...action.meta, | |
app, | |
}, | |
}), | |
}; | |
} | |
render() { | |
return Children.only(this.props.children); | |
} | |
} | |
ReactDOM.render( | |
<Provider store={store}> | |
<NameSpaceProvider app="todomvc1"><App/></NameSpaceProvider> | |
<NameSpaceProvider app="todomvc2"><App/></NameSpaceProvider> | |
</Provider>, body); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment