Skip to content

Don't throw error if the store isn't in the context #38

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 1 commit into from
Closed
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
28 changes: 17 additions & 11 deletions src/components/createConnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ function getDisplayName(Component) {
return Component.displayName || Component.name || 'Component';
}

const nullStore = {
getState() {},
subscribe() {},
dispatch() {}
};

// Helps track hot reloading.
let nextVersion = 0;

Expand All @@ -34,8 +40,8 @@ export default function createConnect(React) {
// Helps track hot reloading.
const version = nextVersion++;

function computeStateProps(context) {
const state = context.store.getState();
function computeStateProps(store) {
const state = store.getState();
const stateProps = finalMapStateToProps(state);
invariant(
isPlainObject(stateProps),
Expand All @@ -45,8 +51,8 @@ export default function createConnect(React) {
return stateProps;
}

function computeDispatchProps(context) {
const { dispatch } = context.store;
function computeDispatchProps(store) {
const { dispatch } = store;
const dispatchProps = finalMapDispatchToProps(dispatch);
invariant(
isPlainObject(dispatchProps),
Expand All @@ -71,7 +77,7 @@ export default function createConnect(React) {
static DecoratedComponent = DecoratedComponent;

static contextTypes = {
store: storeShape.isRequired
store: storeShape
};

shouldComponentUpdate(nextProps, nextState) {
Expand All @@ -81,15 +87,15 @@ export default function createConnect(React) {
constructor(props, context) {
super(props, context);
this.version = version;
this.store = context.store || nullStore;
this.setUnderlyingRef = ::this.setUnderlyingRef;

this.stateProps = computeStateProps(context);
this.dispatchProps = computeDispatchProps(context);
this.stateProps = computeStateProps(this.store);
this.dispatchProps = computeDispatchProps(this.store);
this.state = this.computeNextState();
}

recomputeStateProps() {
const nextStateProps = computeStateProps(this.context);
const nextStateProps = computeStateProps(this.store);
if (shallowEqual(nextStateProps, this.stateProps)) {
return false;
}
Expand All @@ -99,7 +105,7 @@ export default function createConnect(React) {
}

recomputeDispatchProps() {
const nextDispatchProps = computeDispatchProps(this.context);
const nextDispatchProps = computeDispatchProps(this.store);
if (shallowEqual(nextDispatchProps, this.dispatchProps)) {
return false;
}
Expand Down Expand Up @@ -129,7 +135,7 @@ export default function createConnect(React) {

trySubscribe() {
if (shouldSubscribe && !this.unsubscribe) {
this.unsubscribe = this.context.store.subscribe(::this.handleChange);
this.unsubscribe = this.store.subscribe(::this.handleChange);
this.handleChange();
}
}
Expand Down