Skip to content

Instantly share code, notes, and snippets.

@nelix
Created March 10, 2017 00:49
Show Gist options
  • Save nelix/49e49d8046036e1bcffcfd6768a2945b to your computer and use it in GitHub Desktop.
Save nelix/49e49d8046036e1bcffcfd6768a2945b to your computer and use it in GitHub Desktop.
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