Skip to content

Stateless Component: React.forwardRef make defaultProps invalid #16653

Closed

Description

@gitHber

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

Activity

  1. threepointone commented on Sep 4, 2019

    @threepointone
    Contributor

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

  2. bvaughn commented on Oct 3, 2019

    @bvaughn
    Contributor

    Closing because this issue has insufficient info.

  3. PaulRBerg commented on Nov 21, 2019

    @PaulRBerg

    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.

  4. bvaughn commented on Nov 22, 2019

    @bvaughn
    Contributor

    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?

  5. ronen commented on Dec 11, 2019

    @ronen

    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',
    }
    
  6. nhuthan commented on Mar 8, 2024

    @nhuthan

    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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions