Skip to content
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

Added warning to <Context.Provider> in case no value prop is provided #19054

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/react-reconciler/src/ReactFiberBeginWork.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -2795,6 +2795,8 @@ function updatePortalComponent(
return workInProgress.child;
}

let hasWarnedAboutUsingNoValuePropOnContextProvider = false;

function updateContextProvider(
current: Fiber | null,
workInProgress: Fiber,
Expand All @@ -2809,6 +2811,14 @@ function updateContextProvider(
const newValue = newProps.value;

if (__DEV__) {
if (!('value' in newProps)) {
if (!hasWarnedAboutUsingNoValuePropOnContextProvider) {
hasWarnedAboutUsingNoValuePropOnContextProvider = true;
console.error(
'The prop `value` is required in `Context.Provider`, have you misspelled it',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's reword this a bit:

The `value` prop is required for the `<Context.Provider>`. Did you misspell it or forget to pass it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the message 😅 .

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

);
}
}
const providerPropTypes = workInProgress.type.propTypes;

if (providerPropTypes) {
Expand Down
10 changes: 10 additions & 0 deletions packages/react-reconciler/src/ReactFiberBeginWork.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -2792,6 +2792,8 @@ function updatePortalComponent(
return workInProgress.child;
}

let hasWarnedAboutUsingNoValuePropOnContextProvider = false;

function updateContextProvider(
current: Fiber | null,
workInProgress: Fiber,
Expand All @@ -2806,6 +2808,14 @@ function updateContextProvider(
const newValue = newProps.value;

if (__DEV__) {
if (!('value' in newProps)) {
if (!hasWarnedAboutUsingNoValuePropOnContextProvider) {
hasWarnedAboutUsingNoValuePropOnContextProvider = true;
console.error(
'The prop `value` is required in `Context.Provider`, have you misspelled it',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

);
}
}
const providerPropTypes = workInProgress.type.propTypes;

if (providerPropTypes) {
Expand Down
17 changes: 16 additions & 1 deletion packages/react-reconciler/src/__tests__/ReactNewContext-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,21 @@ describe('ReactNewContext', () => {
);
});

it('warns if no value prop provided', () => {
const Context = React.createContext();

ReactNoop.render(
<Context.Provider anyPropNameOtherThanValue="value could be anything" />,
);

expect(() => expect(Scheduler).toFlushWithoutYielding()).toErrorDev(
'The prop `value` is required in `Context.Provider`, have you misspelled it',
{
withoutStack: true,
},
);
});

it('warns if multiple renderers concurrently render the same context', () => {
spyOnDev(console, 'error');
const Context = React.createContext(0);
Expand Down Expand Up @@ -1617,7 +1632,7 @@ describe('ReactNewContext', () => {
// caused by unwinding the context from wrong point.
ReactNoop.render(
<errorInCompletePhase>
<Context.Provider />
<Context.Provider value={null} />
</errorInCompletePhase>,
);
expect(Scheduler).toFlushAndThrow('Error in host config.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ describe('ReactTestRenderer', () => {
const Context = React.createContext(null);
const Indirection = React.Fragment;
const App = () => (
<Context.Provider>
<Context.Provider value={null}>
<Indirection>
<Context.Consumer>{() => null}</Context.Consumer>
</Indirection>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ describe('ReactTestRendererTraversal', () => {
).toBe(2);
expect(
ReactTestRenderer.create(
<Context.Provider>
<Context.Provider value={null}>
<div />
<div />
</Context.Provider>,
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/__tests__/ReactContextValidator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,13 @@ describe('ReactContextValidator', () => {

class Component extends React.Component {
render() {
return <TestContext.Provider />;
return <TestContext.Provider value={null} />;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this value={undefined} instead and you won't need to change the message.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now using undefined and reverted message

}
}

expect(() => ReactTestUtils.renderIntoDocument(<Component />)).toErrorDev(
'Warning: Failed prop type: The prop `value` is marked as required in ' +
'`Context.Provider`, but its value is `undefined`.\n' +
'`Context.Provider`, but its value is `null`.\n' +
' in Component (at **)',
);
});
Expand Down