-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Support getChildContext() in shallow render #1971
Support getChildContext() in shallow render #1971
Conversation
a301c92
to
56d78bf
Compare
P.S. I'm pretty sure the build will succeed once is #1968 is in. |
56d78bf
to
5a4f92b
Compare
5a4f92b
to
f0cd7e2
Compare
@minznerjosh I've rebased this and split up the commits a bit more atomically; it'd be great if you could add some tests to each commit/change :-) |
@ljharb you’ve got it! I’ll take care of it when I get back from vacation on Wednesday. Thanks for taking a look at this! |
f426448
to
ddf3550
Compare
@ljharb I added tests to every commit except |
} | ||
const childContext = instance.getChildContext(); | ||
Object.keys(childContext).forEach((key) => { | ||
if (!(key in Component.childContextTypes)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are we using in
here because React does? it seems like this should be an own property check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I tried to match react as much as possible and they are using in
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add a comment then, above line 193, linking to https://github.com/facebook/react/blob/1454a8be03794f5e0b23a7e7696cbbbdcf8b0f5d/packages/react-dom/src/server/ReactPartialRenderer.js#L630-L637
ddf3550
to
cc59eab
Compare
cc59eab
to
47b860f
Compare
This is ready for another round of review! |
47b860f
to
f12f0dc
Compare
This is ready for another round! |
@minznerjosh is there any chance the functionality is actually accessible inside one of the react packages, without copy/paste? |
@ljharb Unfortunately, I don't think so. 🙁Both pieces of logic live in the |
This comment has been minimized.
This comment has been minimized.
@ljharb Can we live with the duplication of react logic here? If so, anything else blocking? |
It's not ideal, but we can live with it :-) I'm reviewing and making some tweaks locally now. |
Thank you!! Lemme know if there’s anything else I can do to help. |
9fba55d
to
02d6337
Compare
Thanks for fixing this long-standing bug! |
- [new] add `legacyContextMode`, and `getChildContext` lifecycle method (#1971) - [deps] update `enzyme-adapter-utils`, `object.values`, `react-is`, `enzyme-adapter-react-helper` - [dev deps] update `eslint`, `eslint-plugin-react`, `rimraf`, `eslint-plugin-import`, `eslint-plugin-jsx-a11y`
- [new] add `checkPropTypes` (#1971) - [new] add `legacyContextMode`, and `getChildContext` lifecycle method (#1971) - [fix] Filter legacy context on childContextTypes when necessary (#1968) - [deps] update `enzyme-adapter-utils`, `react-is` - [dev deps] update `eslint-plugin-import`, `eslint`, `eslint-plugin-react`, `rimraf`, `eslint-plugin-react`, `eslint-plugin-jsx-a11y`
- [new] add `checkPropTypes` (#1971) - [new] add `legacyContextMode`, and `getChildContext` lifecycle method (#1971) - [fix] Filter legacy context on childContextTypes when necessary (#1968) - [deps] update `enzyme-adapter-utils`, `react-is`, `object.values` - [dev deps] update `eslint-plugin-import`, `eslint`, `eslint-plugin-react`, `rimraf`, `eslint-plugin-react`, `eslint-plugin-jsx-a11y`
- [new] add `checkPropTypes` (#1971) - [new] add `legacyContextMode`, and `getChildContext` lifecycle method (#1971) - [fix] Filter legacy context on childContextTypes when necessary (#1968) - [deps] update `enzyme-adapter-utils`, `react-is`, `object.values` - [dev deps] update `eslint-plugin-import`, `eslint`, `eslint-plugin-react`, `rimraf`, `eslint-plugin-react`, `eslint-plugin-jsx-a11y`
- [new] add `checkPropTypes` (#1971) - [new] add `legacyContextMode`, and `getChildContext` lifecycle method (#1971) - [fix] Filter legacy context on childContextTypes when necessary (#1968) - [deps] update `enzyme-adapter-utils`, `react-is`, `object.values` - [dev deps] update `eslint-plugin-import`, `eslint`, `eslint-plugin-react`, `rimraf`, `eslint-plugin-react`, `eslint-plugin-jsx-a11y`
Fixes #664
This PR adds support for
getChildContext()
when shallow-rendering. I've decided to only implement parent-based context, not owner-based context (as I'm not exactly sure what that behavior is, tbh) which means I've excludedreact@0.13
from this implementation. (Hope that's okay!)In
react@<=15
, the react shallow renderer actually callsgetChildContext()
for us! This means, as is the case withshouldComponentUpdate()
, we just spy on that method's return value.In
react@>=16
,getChildContext()
is not called (understandable—it's a deprecated API), so we have to call it ourselves.P.S. Sorry the diff is a little ugly. I ended up messing with the nesting of some conditionals to avoid duplicated checks.