Skip to content
Merged
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@
"mocha-jsdom": "~0.4.0",
"react": "^0.14.0-beta3",
"react-addons-test-utils": "^0.14.0-beta3",
"redux": "^1.0.1",
"redux": "^2.0.0",
"rimraf": "^2.3.4",
"webpack": "^1.11.0"
},
"dependencies": {
"invariant": "^2.0.0"
},
"peerDependencies": {
"redux": "^1.0.0 || 1.0.0-rc"
"redux": "^2.0.0"
}
}
43 changes: 29 additions & 14 deletions src/components/createProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,47 @@ export default function createProvider(React) {
const storeShape = createStoreShape(PropTypes);
const requireFunctionChild = isUsingOwnerContext(React);

let didWarn = false;
function warnAboutFunction() {
if (didWarn || requireFunctionChild) {
let didWarnAboutChild = false;
function warnAboutFunctionChild() {
if (didWarnAboutChild || requireFunctionChild) {
return;
}

didWarn = true;
didWarnAboutChild = true;
console.error( // eslint-disable-line no-console
'With React 0.14 and later versions, you no longer need to ' +
'wrap <Provider> child into a function.'
);
}
function warnAboutElement() {
if (didWarn || !requireFunctionChild) {
function warnAboutElementChild() {
if (didWarnAboutChild || !requireFunctionChild) {
return;
}

didWarn = true;
didWarnAboutChild = true;
console.error( // eslint-disable-line no-console
'With React 0.13, you need to ' +
'wrap <Provider> child into a function. ' +
'This restriction will be removed with React 0.14.'
);
}

let didWarnAboutReceivingStore = false;
function warnAboutReceivingStore() {
if (didWarnAboutReceivingStore) {
return;
}

didWarnAboutReceivingStore = true;
console.error( // eslint-disable-line no-console
'<Provider> does not support changing `store` on the fly. ' +
'It is most likely that you see this error because you updated to ' +
'Redux 2.x and React Redux 2.x which no longer hot reload reducers ' +
'automatically. See https://github.com/rackt/react-redux/releases/' +
'tag/v2.0.0 for the migration instructions.'
);
}

return class Provider extends Component {
static childContextTypes = {
store: storeShape.isRequired
Expand All @@ -57,32 +73,31 @@ export default function createProvider(React) {
};

getChildContext() {
return { store: this.state.store };
return { store: this.store };
}

constructor(props, context) {
super(props, context);
this.state = { store: props.store };
this.store = props.store;
}

componentWillReceiveProps(nextProps) {
const { store } = this.state;
const { store } = this;
const { store: nextStore } = nextProps;

if (store !== nextStore) {
const nextReducer = nextStore.getReducer();
store.replaceReducer(nextReducer);
warnAboutReceivingStore();
}
}

render() {
let { children } = this.props;

if (typeof children === 'function') {
warnAboutFunction();
warnAboutFunctionChild();
children = children();
} else {
warnAboutElement();
warnAboutElementChild();
}

return Children.only(children);
Expand Down
32 changes: 20 additions & 12 deletions test/components/Provider.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ describe('React', () => {
expect(child.context.store).toBe(store);
});

it('should replace just the reducer when receiving a new store in props', () => {
it('should warn once when receiving a new store in props', () => {
const store1 = createStore((state = 10) => state + 1);
const store2 = createStore((state = 10) => state * 2);
const spy = expect.createSpy(() => ({}));
const store3 = createStore((state = 10) => state * state);

class ProviderContainer extends Component {
state = { store: store1 };
Expand All @@ -192,18 +192,26 @@ describe('React', () => {
const child = TestUtils.findRenderedComponentWithType(container, Child);
expect(child.context.store.getState()).toEqual(11);

child.context.store.subscribe(spy);
child.context.store.dispatch({});
expect(spy.calls.length).toEqual(1);
expect(child.context.store.getState()).toEqual(12);

let spy = expect.spyOn(console, 'error');
container.setState({ store: store2 });
expect(spy.calls.length).toEqual(2);
expect(child.context.store.getState()).toEqual(24);
spy.destroy();

expect(child.context.store.getState()).toEqual(11);
expect(spy.calls.length).toBe(1);
expect(spy.calls[0].arguments[0]).toBe(
'<Provider> does not support changing `store` on the fly. ' +
'It is most likely that you see this error because you updated to ' +
'Redux 2.x and React Redux 2.x which no longer hot reload reducers ' +
'automatically. See https://github.com/rackt/react-redux/releases/' +
'tag/v2.0.0 for the migration instructions.'
);

child.context.store.dispatch({});
expect(spy.calls.length).toEqual(3);
expect(child.context.store.getState()).toEqual(48);
spy = expect.spyOn(console, 'error');
container.setState({ store: store3 });
spy.destroy();

expect(child.context.store.getState()).toEqual(11);
expect(spy.calls.length).toBe(0);
});
});
});