Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/Root.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import createDispatcher from './createDispatcher';

export default class ReduxRoot {
static propTypes = {
stores: PropTypes.object.isRequired,
dispatcher: PropTypes.object,
stores: PropTypes.object,
children: PropTypes.func.isRequired
};

Expand All @@ -12,12 +13,19 @@ export default class ReduxRoot {
};

constructor(props) {
this.dispatcher = createDispatcher();
this.dispatcher.receiveStores(props.stores);
this.dispatcher = props.dispatcher || createDispatcher();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't createDispatcher() be called in static defaultProps?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't good for two reasons:

  • Dispatcher from defaultProps will be shared between all Root instances.
  • It will be created even if we don't use it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, you're right 👍

if (props.stores) {
this.dispatcher.receiveStores(props.stores);
}
}

componentWillReceiveProps(nextProps) {
this.dispatcher.receiveStores(nextProps.stores);
if (nextProps.dispatcher) {
this.dispatcher = nextProps.dispatcher;
}
if (nextProps.stores) {
this.dispatcher.receiveStores(nextProps.stores);
}
}

getChildContext() {
Expand Down
4 changes: 2 additions & 2 deletions src/addons/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import React from 'react';
import Root from '../Root';
import getDisplayName from './getDisplayName';

export default function root(stores) {
export default function root(stores, dispatcher = null) {
return DecoratedComponent => class ReduxRootDecorator {
static displayName = `ReduxRoot(${getDisplayName(DecoratedComponent)})`;

render() {
return (
<Root stores={stores}>
<Root dispatcher={dispatcher} stores={stores}>
{props => <DecoratedComponent {...this.props} {...props} />}
</Root>
);
Expand Down