Skip to content

Commit

Permalink
Add forwardRef DEV warning for prop-types on render function (faceboo…
Browse files Browse the repository at this point in the history
  • Loading branch information
bvaughn authored and rhagigi committed Apr 19, 2018
1 parent cd21a24 commit 169bc35
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/react/src/__tests__/forwardRef-test.internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,25 @@ describe('forwardRef', () => {
'forwardRef requires a render function but was given undefined.',
);
});

it('should warn if the render function provided has propTypes or defaultProps attributes', () => {
function renderWithPropTypes() {
return null;
}
renderWithPropTypes.propTypes = {};

function renderWithDefaultProps() {
return null;
}
renderWithDefaultProps.defaultProps = {};

expect(() => React.forwardRef(renderWithPropTypes)).toWarnDev(
'forwardRef render functions do not support propTypes or defaultProps. ' +
'Did you accidentally pass a React component?',
);
expect(() => React.forwardRef(renderWithDefaultProps)).toWarnDev(
'forwardRef render functions do not support propTypes or defaultProps. ' +
'Did you accidentally pass a React component?',
);
});
});
8 changes: 8 additions & 0 deletions packages/react/src/forwardRef.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ export default function forwardRef<Props, ElementType: React$ElementType>(
'forwardRef requires a render function but was given %s.',
render === null ? 'null' : typeof render,
);

if (render != null) {
warning(
render.defaultProps == null && render.propTypes == null,
'forwardRef render functions do not support propTypes or defaultProps. ' +
'Did you accidentally pass a React component?',
);
}
}

return {
Expand Down

0 comments on commit 169bc35

Please sign in to comment.