From 3487225ea9f65babcd42198910ed4234e7abebb3 Mon Sep 17 00:00:00 2001 From: jdecroock Date: Wed, 11 Sep 2019 00:55:50 +0200 Subject: [PATCH 1/5] (fix) - rerender on ref equal context --- src/create-context.js | 3 --- test/browser/createContext.test.js | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/create-context.js b/src/create-context.js index eb03d6e5c5..bf112ac5ba 100644 --- a/src/create-context.js +++ b/src/create-context.js @@ -13,9 +13,6 @@ export function createContext(defaultValue) { _id: '__cC' + i++, _defaultValue: defaultValue, Consumer(props, context) { - this.shouldComponentUpdate = function (_props, _state, _context) { - return _context !== context; - }; return props.children(context); }, Provider(props) { diff --git a/test/browser/createContext.test.js b/test/browser/createContext.test.js index b14587d29c..3dd090788e 100644 --- a/test/browser/createContext.test.js +++ b/test/browser/createContext.test.js @@ -351,7 +351,7 @@ describe('createContext', () => { expect(scratch.innerHTML).to.equal('
b - 1
a - 1
'); }); - it('should not re-render the consumer if the context doesn\'t change', () => { + it.skip('should not re-render the consumer if the context doesn\'t change', () => { const { Provider, Consumer } = createContext(); const CONTEXT = { i: 1 }; From c14de66c5572a2a172e78b11fb296984e9e76750 Mon Sep 17 00:00:00 2001 From: cristianbote Date: Wed, 11 Sep 2019 09:38:11 +0300 Subject: [PATCH 2/5] Add the check for children --- src/create-context.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/create-context.js b/src/create-context.js index bf112ac5ba..940cc049f2 100644 --- a/src/create-context.js +++ b/src/create-context.js @@ -13,6 +13,11 @@ export function createContext(defaultValue) { _id: '__cC' + i++, _defaultValue: defaultValue, Consumer(props, context) { + + this.shouldComponentUpdate = function (_props, _state, _context) { + return _context !== context || this.props.children !== _props.children; + }; + return props.children(context); }, Provider(props) { From 0749bb24c770b22a706732f998195447a0a9cd6a Mon Sep 17 00:00:00 2001 From: cristianbote Date: Wed, 11 Sep 2019 09:47:31 +0300 Subject: [PATCH 3/5] Added a test case for children as well --- src/create-context.js | 2 +- test/browser/createContext.test.js | 47 +++++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/create-context.js b/src/create-context.js index 940cc049f2..d52f553d7e 100644 --- a/src/create-context.js +++ b/src/create-context.js @@ -16,7 +16,7 @@ export function createContext(defaultValue) { this.shouldComponentUpdate = function (_props, _state, _context) { return _context !== context || this.props.children !== _props.children; - }; + }; return props.children(context); }, diff --git a/test/browser/createContext.test.js b/test/browser/createContext.test.js index 3dd090788e..7dea405a2a 100644 --- a/test/browser/createContext.test.js +++ b/test/browser/createContext.test.js @@ -351,7 +351,7 @@ describe('createContext', () => { expect(scratch.innerHTML).to.equal('
b - 1
a - 1
'); }); - it.skip('should not re-render the consumer if the context doesn\'t change', () => { + it('should not re-render the consumer if the context doesn\'t change', () => { const { Provider, Consumer } = createContext(); const CONTEXT = { i: 1 }; @@ -363,10 +363,12 @@ describe('createContext', () => { sinon.spy(Inner.prototype, 'render'); + const Child = data => ; + render( - {data => } + {Child} , scratch @@ -375,7 +377,7 @@ describe('createContext', () => { render( - {data => } + {Child} , scratch @@ -388,7 +390,7 @@ describe('createContext', () => { render( - {data => } + {Child} , scratch @@ -399,6 +401,43 @@ describe('createContext', () => { expect(scratch.innerHTML).to.equal('
2
'); }); + + + 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
{props.i}
; + } + } + + sinon.spy(Inner.prototype, 'render'); + + render( + + + {data => } + + , + scratch + ); + + render( + + + {data => } + + , + 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('
1
'); + }); + describe('class.contextType', () => { it('should use default value', () => { const ctx = createContext('foo'); From b45b36ca0285510e1dac0fd36de4f6c598f21170 Mon Sep 17 00:00:00 2001 From: cristianbote Date: Wed, 11 Sep 2019 09:52:38 +0300 Subject: [PATCH 4/5] Remove the scope and use the closure --- src/create-context.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/create-context.js b/src/create-context.js index d52f553d7e..36a17e936e 100644 --- a/src/create-context.js +++ b/src/create-context.js @@ -15,7 +15,7 @@ export function createContext(defaultValue) { Consumer(props, context) { this.shouldComponentUpdate = function (_props, _state, _context) { - return _context !== context || this.props.children !== _props.children; + return _context !== context || props.children !== _props.children; }; return props.children(context); From 9bd6c7f591359fb7b16a1692dbf8dbbb06e7ec88 Mon Sep 17 00:00:00 2001 From: cristianbote Date: Wed, 11 Sep 2019 10:12:01 +0300 Subject: [PATCH 5/5] Fix the linting --- test/browser/createContext.test.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/browser/createContext.test.js b/test/browser/createContext.test.js index 7dea405a2a..f44747230d 100644 --- a/test/browser/createContext.test.js +++ b/test/browser/createContext.test.js @@ -401,8 +401,6 @@ describe('createContext', () => { expect(scratch.innerHTML).to.equal('
2
'); }); - - it('should re-render the consumer if the children change', () => { const { Provider, Consumer } = createContext(); const CONTEXT = { i: 1 }; @@ -418,7 +416,7 @@ describe('createContext', () => { render( - {data => } + {data => } , scratch @@ -427,7 +425,7 @@ describe('createContext', () => { render( - {data => } + {data => } , scratch