-
Notifications
You must be signed in to change notification settings - Fork 47.3k
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
Validate props on context providers #12658
Changes from 11 commits
1193a94
1184974
2547b22
b596129
b26897e
ed42cac
63ef731
ea50645
683fdfe
5a38c06
bdfab1f
ab53c91
c500f24
af05d8e
dd0ea94
c419075
79ea1b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ import type {NewContext} from './ReactFiberNewContext'; | |
import type {HydrationContext} from './ReactFiberHydrationContext'; | ||
import type {FiberRoot} from './ReactFiberRoot'; | ||
import type {ExpirationTime} from './ReactFiberExpirationTime'; | ||
import emptyFunction from 'fbjs/lib/emptyFunction'; | ||
import checkPropTypes from 'prop-types/checkPropTypes'; | ||
|
||
import { | ||
IndeterminateComponent, | ||
|
@@ -63,14 +65,18 @@ import {NoWork, Never} from './ReactFiberExpirationTime'; | |
import {AsyncMode, StrictMode} from './ReactTypeOfMode'; | ||
import MAX_SIGNED_31_BIT_INT from './maxSigned31BitInt'; | ||
|
||
const {getCurrentFiberStackAddendum} = ReactDebugCurrentFiber; | ||
|
||
let didWarnAboutBadClass; | ||
let didWarnAboutGetDerivedStateOnFunctionalComponent; | ||
let didWarnAboutStatelessRefs; | ||
let getStack = emptyFunction.thatReturns(''); | ||
|
||
if (__DEV__) { | ||
didWarnAboutBadClass = {}; | ||
didWarnAboutGetDerivedStateOnFunctionalComponent = {}; | ||
didWarnAboutStatelessRefs = {}; | ||
getStack = getCurrentFiberStackAddendum; | ||
} | ||
|
||
export default function<T, P, I, TI, HI, PI, C, CC, CX, PL>( | ||
|
@@ -885,6 +891,18 @@ export default function<T, P, I, TI, HI, PI, C, CC, CX, PL>( | |
const newValue = newProps.value; | ||
workInProgress.memoizedProps = newProps; | ||
|
||
const providerPropTypes = workInProgress.type.propTypes; | ||
|
||
if (__DEV__ && providerPropTypes) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please split this into two nested conditions? We try to always keep |
||
checkPropTypes( | ||
providerPropTypes, | ||
newProps, | ||
'prop', | ||
'Context.Provider', | ||
getStack, | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is missing the component stack argument. Can you please add it, similar to how we do elsewhere? I think you can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahhhh I meant to do this after I was done testing it and completely forgot, I’ll get on that tonight! |
||
} | ||
|
||
let changedBits: number; | ||
if (oldProps === null) { | ||
// Initial render | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -225,6 +225,23 @@ describe('ReactContextValidator', () => { | |
ReactTestUtils.renderIntoDocument(<Component testContext={{foo: 'foo'}} />); | ||
}); | ||
|
||
it('warns of incorrect prop types on context provider', () => { | ||
const TestContext = React.createContext(); | ||
|
||
TestContext.Provider.propTypes = { | ||
value: PropTypes.string.isRequired, | ||
}; | ||
|
||
ReactTestUtils.renderIntoDocument(<TestContext.Provider value="val" />); | ||
|
||
expect(() => | ||
ReactTestUtils.renderIntoDocument(<TestContext.Provider />), | ||
).toWarnDev( | ||
'Warning: Failed prop type: The prop `value` is marked as required in ' + | ||
'`Context.Provider`, but its value is `undefined`.', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please include stack in the expected message. You’ll want to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’m having a bit of trouble finding an example of this in the tests. Potentially just not sure what I’m looking for. Do you know of one that uses this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nicolevy you can find an example of what Dan means here: https://github.com/facebook/react/blob/master/packages/react/src/__tests__/ReactContextValidator-test.js#L182-L183 |
||
); | ||
}); | ||
|
||
// TODO (bvaughn) Remove this test and the associated behavior in the future. | ||
// It has only been added in Fiber to match the (unintentional) behavior in Stack. | ||
it('should warn (but not error) if getChildContext method is missing', () => { | ||
|
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.
getStack
would only ever be called in DEV, so we can passgetCurrentFiberStackAddendum
tocheckPropTypes
directly and avoid thegetStack
variable altogether.