Skip to content
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

(fix) - rerender on ref equal context #1925

Merged
merged 5 commits into from
Sep 11, 2019
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: 3 additions & 1 deletion src/create-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ export function createContext(defaultValue) {
_id: '__cC' + i++,
_defaultValue: defaultValue,
Consumer(props, context) {

this.shouldComponentUpdate = function (_props, _state, _context) {
return _context !== context;
return _context !== context || props.children !== _props.children;
};

return props.children(context);
},
Provider(props) {
Expand Down
43 changes: 40 additions & 3 deletions test/browser/createContext.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,12 @@ describe('createContext', () => {

sinon.spy(Inner.prototype, 'render');

const Child = data => <Inner {...data} />;

render(
<Provider value={CONTEXT}>
<Consumer>
{data => <Inner {...data} />}
{Child}
</Consumer>
</Provider>,
scratch
Expand All @@ -375,7 +377,7 @@ describe('createContext', () => {
render(
<Provider value={CONTEXT}>
<Consumer>
{data => <Inner {...data} />}
{Child}
</Consumer>
</Provider>,
scratch
Expand All @@ -388,7 +390,7 @@ describe('createContext', () => {
render(
<Provider value={{ i: 2 }}>
<Consumer>
{data => <Inner {...data} />}
{Child}
</Consumer>
</Provider>,
scratch
Expand All @@ -399,6 +401,41 @@ describe('createContext', () => {
expect(scratch.innerHTML).to.equal('<div>2</div>');
});

it('should re-render the consumer if the children change', () => {
const { Provider, Consumer } = createContext();
const CONTEXT = { i: 1 };

class Inner extends Component {
render(props) {
return <div>{props.i}</div>;
}
}

sinon.spy(Inner.prototype, 'render');

render(
<Provider value={CONTEXT}>
<Consumer>
{data => <Inner {...data} />}
</Consumer>
</Provider>,
scratch
);

render(
<Provider value={CONTEXT}>
<Consumer>
{data => <Inner {...data} />}
</Consumer>
</Provider>,
scratch
);

// Rendered twice, with two different children for consumer, should render twice
expect(Inner.prototype.render).to.have.been.calledTwice;
expect(scratch.innerHTML).to.equal('<div>1</div>');
});

describe('class.contextType', () => {
it('should use default value', () => {
const ctx = createContext('foo');
Expand Down