Skip to content

Commit f4097c1

Browse files
author
Shailendra Gupta
authored
Added warning to <Context.Provider> in case no value prop is provided (facebook#19054)
* added warning to context.provider in case no value prop * update message * updated message and pass undefined
1 parent 47ff31a commit f4097c1

File tree

6 files changed

+39
-4
lines changed

6 files changed

+39
-4
lines changed

packages/react-reconciler/src/ReactFiberBeginWork.new.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2804,6 +2804,8 @@ function updatePortalComponent(
28042804
return workInProgress.child;
28052805
}
28062806

2807+
let hasWarnedAboutUsingNoValuePropOnContextProvider = false;
2808+
28072809
function updateContextProvider(
28082810
current: Fiber | null,
28092811
workInProgress: Fiber,
@@ -2818,6 +2820,14 @@ function updateContextProvider(
28182820
const newValue = newProps.value;
28192821

28202822
if (__DEV__) {
2823+
if (!('value' in newProps)) {
2824+
if (!hasWarnedAboutUsingNoValuePropOnContextProvider) {
2825+
hasWarnedAboutUsingNoValuePropOnContextProvider = true;
2826+
console.error(
2827+
'The `value` prop is required for the `<Context.Provider>`. Did you misspell it or forget to pass it?',
2828+
);
2829+
}
2830+
}
28212831
const providerPropTypes = workInProgress.type.propTypes;
28222832

28232833
if (providerPropTypes) {

packages/react-reconciler/src/ReactFiberBeginWork.old.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2804,6 +2804,8 @@ function updatePortalComponent(
28042804
return workInProgress.child;
28052805
}
28062806

2807+
let hasWarnedAboutUsingNoValuePropOnContextProvider = false;
2808+
28072809
function updateContextProvider(
28082810
current: Fiber | null,
28092811
workInProgress: Fiber,
@@ -2818,6 +2820,14 @@ function updateContextProvider(
28182820
const newValue = newProps.value;
28192821

28202822
if (__DEV__) {
2823+
if (!('value' in newProps)) {
2824+
if (!hasWarnedAboutUsingNoValuePropOnContextProvider) {
2825+
hasWarnedAboutUsingNoValuePropOnContextProvider = true;
2826+
console.error(
2827+
'The `value` prop is required for the `<Context.Provider>`. Did you misspell it or forget to pass it?',
2828+
);
2829+
}
2830+
}
28212831
const providerPropTypes = workInProgress.type.propTypes;
28222832

28232833
if (providerPropTypes) {

packages/react-reconciler/src/__tests__/ReactNewContext-test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,21 @@ describe('ReactNewContext', () => {
10831083
);
10841084
});
10851085

1086+
it('warns if no value prop provided', () => {
1087+
const Context = React.createContext();
1088+
1089+
ReactNoop.render(
1090+
<Context.Provider anyPropNameOtherThanValue="value could be anything" />,
1091+
);
1092+
1093+
expect(() => expect(Scheduler).toFlushWithoutYielding()).toErrorDev(
1094+
'The `value` prop is required for the `<Context.Provider>`. Did you misspell it or forget to pass it?',
1095+
{
1096+
withoutStack: true,
1097+
},
1098+
);
1099+
});
1100+
10861101
it('warns if multiple renderers concurrently render the same context', () => {
10871102
spyOnDev(console, 'error');
10881103
const Context = React.createContext(0);
@@ -1611,7 +1626,7 @@ describe('ReactNewContext', () => {
16111626
// caused by unwinding the context from wrong point.
16121627
ReactNoop.render(
16131628
<errorInCompletePhase>
1614-
<Context.Provider />
1629+
<Context.Provider value={null} />
16151630
</errorInCompletePhase>,
16161631
);
16171632
expect(Scheduler).toFlushAndThrow('Error in host config.');

packages/react-test-renderer/src/__tests__/ReactTestRenderer-test.internal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ describe('ReactTestRenderer', () => {
10121012
const Context = React.createContext(null);
10131013
const Indirection = React.Fragment;
10141014
const App = () => (
1015-
<Context.Provider>
1015+
<Context.Provider value={null}>
10161016
<Indirection>
10171017
<Context.Consumer>{() => null}</Context.Consumer>
10181018
</Indirection>

packages/react-test-renderer/src/__tests__/ReactTestRendererTraversal-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ describe('ReactTestRendererTraversal', () => {
236236
).toBe(2);
237237
expect(
238238
ReactTestRenderer.create(
239-
<Context.Provider>
239+
<Context.Provider value={null}>
240240
<div />
241241
<div />
242242
</Context.Provider>,

packages/react/src/__tests__/ReactContextValidator-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ describe('ReactContextValidator', () => {
263263

264264
class Component extends React.Component {
265265
render() {
266-
return <TestContext.Provider />;
266+
return <TestContext.Provider value={undefined} />;
267267
}
268268
}
269269

0 commit comments

Comments
 (0)