Skip to content

Commit 49433b3

Browse files
authored
support passing context into pure component (#4269)
1 parent 79be156 commit 49433b3

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

compat/src/PureComponent.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import { shallowDiffers } from './util';
44
/**
55
* Component class with a predefined `shouldComponentUpdate` implementation
66
*/
7-
export function PureComponent(p) {
7+
export function PureComponent(p, c) {
88
this.props = p;
9+
this.context = c;
910
}
1011
PureComponent.prototype = new Component();
1112
// Some third-party libraries check if this property is present

compat/test/browser/PureComponent.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,38 @@ describe('PureComponent', () => {
3737
expect(spy).to.be.calledWithMatch(expected, expected);
3838
});
3939

40+
it('should pass context in constructor', () => {
41+
let instance;
42+
// Not initializing state matches React behavior: https://codesandbox.io/s/rml19v8o2q
43+
class Foo extends React.PureComponent {
44+
constructor(props, context) {
45+
super(props, context);
46+
expect(this.props).to.equal(props);
47+
expect(this.state).to.deep.equal(undefined);
48+
expect(this.context).to.equal(context);
49+
50+
instance = this;
51+
}
52+
render(props) {
53+
return <div {...props}>Hello</div>;
54+
}
55+
}
56+
57+
sinon.spy(Foo.prototype, 'render');
58+
59+
const PROPS = { foo: 'bar' };
60+
React.render(<Foo {...PROPS} />, scratch);
61+
62+
expect(Foo.prototype.render)
63+
.to.have.been.calledOnce.and.to.have.been.calledWithMatch(PROPS, {}, {})
64+
.and.to.have.returned(sinon.match({ type: 'div', props: PROPS }));
65+
expect(instance.props).to.deep.equal(PROPS);
66+
expect(instance.state).to.deep.equal({});
67+
expect(instance.context).to.deep.equal({});
68+
69+
expect(scratch.innerHTML).to.equal('<div foo="bar">Hello</div>');
70+
});
71+
4072
it('should ignore the __source variable', () => {
4173
const pureSpy = sinon.spy();
4274
const appSpy = sinon.spy();

0 commit comments

Comments
 (0)