Skip to content

Commit

Permalink
Added warning to <Context.Provider> in case no value prop is provided (
Browse files Browse the repository at this point in the history
…#19054)

* added warning to context.provider in case no value prop

* update message

* updated message and pass undefined
  • Loading branch information
Shailendra Gupta authored Jun 30, 2020
1 parent 47ff31a commit f4097c1
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 4 deletions.
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 @@ -2804,6 +2804,8 @@ function updatePortalComponent(
return workInProgress.child;
}

let hasWarnedAboutUsingNoValuePropOnContextProvider = false;

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

if (__DEV__) {
if (!('value' in newProps)) {
if (!hasWarnedAboutUsingNoValuePropOnContextProvider) {
hasWarnedAboutUsingNoValuePropOnContextProvider = true;
console.error(
'The `value` prop is required for the `<Context.Provider>`. Did you misspell it or forget to pass it?',
);
}
}
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 @@ -2804,6 +2804,8 @@ function updatePortalComponent(
return workInProgress.child;
}

let hasWarnedAboutUsingNoValuePropOnContextProvider = false;

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

if (__DEV__) {
if (!('value' in newProps)) {
if (!hasWarnedAboutUsingNoValuePropOnContextProvider) {
hasWarnedAboutUsingNoValuePropOnContextProvider = true;
console.error(
'The `value` prop is required for the `<Context.Provider>`. Did you misspell it or forget to pass it?',
);
}
}
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 @@ -1083,6 +1083,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 `value` prop is required for the `<Context.Provider>`. Did you misspell it or forget to pass 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 @@ -1611,7 +1626,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
2 changes: 1 addition & 1 deletion packages/react/src/__tests__/ReactContextValidator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ describe('ReactContextValidator', () => {

class Component extends React.Component {
render() {
return <TestContext.Provider />;
return <TestContext.Provider value={undefined} />;
}
}

Expand Down

0 comments on commit f4097c1

Please sign in to comment.