diff --git a/src/create-context.js b/src/create-context.js index 36a17e936e..7daae35348 100644 --- a/src/create-context.js +++ b/src/create-context.js @@ -13,11 +13,6 @@ export function createContext(defaultValue) { _id: '__cC' + i++, _defaultValue: defaultValue, Consumer(props, context) { - - this.shouldComponentUpdate = function (_props, _state, _context) { - return _context !== context || props.children !== _props.children; - }; - return props.children(context); }, Provider(props) { @@ -27,14 +22,17 @@ export function createContext(defaultValue) { ctx[context._id] = this; return ctx; }; - this.shouldComponentUpdate = props => { - subs.some(c => { - // Check if still mounted - if (c._parentDom) { - c.context = props.value; - enqueueRender(c); - } - }); + this.shouldComponentUpdate = _props => { + if (props.value !== _props.value) { + ctx[context._id].props.value = _props.value; + subs.some(c => { + // Check if still mounted + if (c._parentDom) { + c.context = _props.value; + enqueueRender(c); + } + }); + } }; this.sub = (c) => { subs.push(c); diff --git a/test/browser/createContext.test.js b/test/browser/createContext.test.js index f44747230d..72812aa6c8 100644 --- a/test/browser/createContext.test.js +++ b/test/browser/createContext.test.js @@ -1,4 +1,4 @@ -import { setupRerender } from 'preact/test-utils'; +import { setupRerender, act } from 'preact/test-utils'; import { createElement as h, render, Component, createContext } from '../../src/index'; import { setupScratch, teardown } from '../_util/helpers'; import { Fragment } from '../../src'; @@ -355,6 +355,16 @@ describe('createContext', () => { const { Provider, Consumer } = createContext(); const CONTEXT = { i: 1 }; + class NoUpdate extends Component { + shouldComponentUpdate() { + return false; + } + + render() { + return this.props.children; + } + } + class Inner extends Component { render(props) { return
{value}: {this.state.status}
; + } + + render() { + return ( +value: initial
'); + + act(() => { + app.setState({ status: 'updated' }); + rerender(); + }); + + expect(scratch.innerHTML).to.equal('value: updated
'); + }); + it('should re-render the consumer if the children change', () => { const { Provider, Consumer } = createContext(); const CONTEXT = { i: 1 }; @@ -413,23 +469,27 @@ describe('createContext', () => { sinon.spy(Inner.prototype, 'render'); - render( -