You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Stateless Component: React.forwardRef make defaultProps invalid #16653
When I add React.forwardRef to a stateless component that is already have defaultProps, It make all defaultProps to undefined?
React.forwardRef can't use with defaultProps ?
react-version: 16.8.6
Reacted by Stanislav Mayorov, xing.org1^ and Sergey Shenets
Ah, I see why there's confusion here. The function passed to forwardRef is not a React component. (React components don't accept a second ref param for example.) It's just a function that "renders" (returns) React elements. So it does not support defaultProps. We have tests that confirm this as expected behavior.
React also logs the following error in the console (as can be seen in your Code Sandbox repro):
Warning: forwardRef render functions do not support propTypes or defaultProps. Did you accidentally pass a React component?
I just hit on this too. In case anyone else comes across this thread and needs some extra hand-holding (I was still a bit befuddled and it took me a bit to understand):
The function passed to forwardRef is not a React component.
...but the function returned by forwardRefis effectively a React component. So that's where the propTypes and defaultProps would go. It also needs a displayName. I.e.:
I just hit on this too. In case anyone else comes across this thread and needs some extra hand-holding (I was still a bit befuddled and it took me a bit to understand):
The function passed to forwardRef is not a React component.
...but the function returned by forwardRefis effectively a React component. So that's where the propTypes and defaultProps would go. It also needs a displayName. I.e.:
Activity
threepointone commentedon Sep 4, 2019
Could you please use codesandbox.io to make a reproducing example? Or a git repo I can try locally (but preferred codesandbox). Thanks!
bvaughn commentedon Oct 3, 2019
Closing because this issue has insufficient info.
PaulRBerg commentedon Nov 21, 2019
Here's a CodeSandbox that reproduces the issue.
It seems that eslint-plugin-react throws this error:
But it's confusing, since removing the
propTypes(in my IDE not on CodeSandbox), gets me the "is missing in props validation" warning.bvaughn commentedon Nov 22, 2019
Ah, I see why there's confusion here. The function passed to
forwardRefis not a React component. (React components don't accept a secondrefparam for example.) It's just a function that "renders" (returns) React elements. So it does not supportdefaultProps. We have tests that confirm this as expected behavior.React also logs the following error in the console (as can be seen in your Code Sandbox repro):
ronen commentedon Dec 11, 2019
I just hit on this too. In case anyone else comes across this thread and needs some extra hand-holding (I was still a bit befuddled and it took me a bit to understand):
...but the function returned by
forwardRefis effectively a React component. So that's where thepropTypesanddefaultPropswould go. It also needs adisplayName. I.e.:fix(IconButton): Implementation of forwardRef
fix(IconButton): Implementation of forwardRef
nhuthan commentedon Mar 8, 2024
It doesn't work for react 18