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

Stateless Component: React.forwardRef make defaultProps invalid #16653

Closed
gitHber opened this issue Sep 4, 2019 · 6 comments
Closed

Stateless Component: React.forwardRef make defaultProps invalid #16653

gitHber opened this issue Sep 4, 2019 · 6 comments

Comments

@gitHber
Copy link

gitHber commented Sep 4, 2019

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

@threepointone
Copy link
Contributor

Could you please use codesandbox.io to make a reproducing example? Or a git repo I can try locally (but preferred codesandbox). Thanks!

@bvaughn
Copy link
Contributor

bvaughn commented Oct 3, 2019

Closing because this issue has insufficient info.

@PaulRBerg
Copy link

PaulRBerg commented Nov 21, 2019

Here's a CodeSandbox that reproduces the issue.

It seems that eslint-plugin-react throws this error:

Warning: forwardRef render functions do not support propTypes or defaultProps. Did you accidentally pass a React component?

But it's confusing, since removing the propTypes (in my IDE not on CodeSandbox), gets me the "is missing in props validation" warning.

@bvaughn
Copy link
Contributor

bvaughn commented Nov 22, 2019

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?

@ronen
Copy link

ronen commented 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):

The function passed to forwardRef is not a React component.

...but the function returned by forwardRef is effectively a React component. So that's where the propTypes and defaultProps would go. It also needs a displayName. I.e.:

const MyComponent = React.forwardRef((props, ref) => { 
   ...
})

MyComponent.displayName = 'MyComponent'

MyComponent.propTypes = {
   name: PropTypes.string,
}

MyComponent.defaultProps = {
  name: 'default Name',
}

Merkur39 added a commit to cozy/cozy-ui that referenced this issue May 6, 2022
By wrapping the component when exporting it,
propTypes can't work anymore and we get this kind of warning in console:
```
Warning: forwardRef render functions do not support
propTypes or defaultProps.
Did you accidentally pass a React component?
```

We need to use the forwardRef as implemented on this PR or as here:
https://bit.ly/3ykcWZx

More info: facebook/react#16653
Merkur39 added a commit to cozy/cozy-ui that referenced this issue May 9, 2022
By wrapping the component when exporting it,
propTypes can't work anymore and we get this kind of warning in console:
```
Warning: forwardRef render functions do not support
propTypes or defaultProps.
Did you accidentally pass a React component?
```

We need to use the forwardRef as implemented on this PR or as here:
https://bit.ly/3ykcWZx

More info: facebook/react#16653
@nhuthan
Copy link

nhuthan commented Mar 8, 2024

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 forwardRef is effectively a React component. So that's where the propTypes and defaultProps would go. It also needs a displayName. I.e.:

const MyComponent = React.forwardRef((props, ref) => { 
   ...
})

MyComponent.displayName = 'MyComponent'

MyComponent.propTypes = {
   name: PropTypes.string,
}

MyComponent.defaultProps = {
  name: 'default Name',
}

It doesn't work for react 18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants