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

Enhance dev warnings for forwardRef render function (#13627) #13636

Merged
merged 3 commits into from Sep 13, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 19 additions & 9 deletions packages/react/src/__tests__/forwardRef-test.js
Expand Up @@ -162,22 +162,32 @@ describe('forwardRef', () => {
);
});

it('should warn if the render function provided does not use the forwarded ref parameter', () => {
function arityOfZero() {
return null;
}
it('should not warn if the render function provided does not use any parameter', () => {
const arityOfZero = () => null;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we make this more realistic? Such as a real pass through wrapper. Otherwise intent is not clear.

React.forwardRef(arityOfZero);
});

it('should warn if the render function provided does not use the forwarded ref parameter', () => {
const arityOfOne = props => null;

expect(() => React.forwardRef(arityOfZero)).toWarnDev(
'forwardRef render functions accept two parameters: props and ref. ' +
expect(() => React.forwardRef(arityOfOne)).toWarnDev(
'forwardRef render functions accept exactly two parameters: props and ref. ' +
'Did you forget to use the ref parameter?',
{withoutStack: true},
);
});

expect(() => React.forwardRef(arityOfOne)).toWarnDev(
'forwardRef render functions accept two parameters: props and ref. ' +
'Did you forget to use the ref parameter?',
it('should not warn if the render function provided use exactly two parameters', () => {
const arityOfTwo = (props, ref) => null;
React.forwardRef(arityOfTwo);
});

it('should warn if the render function provided expects to use more than two parameters', () => {
const arityOfThree = (props, ref, x) => null;

expect(() => React.forwardRef(arityOfThree)).toWarnDev(
'forwardRef render functions accept exactly two parameters: props and ref. ' +
'Any additional parameter will be undefined',
{withoutStack: true},
);
});
Expand Down
9 changes: 6 additions & 3 deletions packages/react/src/forwardRef.js
Expand Up @@ -21,9 +21,12 @@ export default function forwardRef<Props, ElementType: React$ElementType>(
);
} else {
warningWithoutStack(
render.length === 2,
'forwardRef render functions accept two parameters: props and ref. ' +
'Did you forget to use the ref parameter?',
// Do not warn for 0 arguments because it could be due to usage of the 'arguments' object
render.length === 0 || render.length === 2,
'forwardRef render functions accept exactly two parameters: props and ref. %s',
render.length === 1
? 'Did you forget to use the ref parameter?'
: 'Any additional parameter will be undefined',
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add period to the sentence.

);
}

Expand Down