Skip to content

Allow to pass custom dispatcher to Root component #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from 2 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