Skip to content

Returning A Function Fails PropTypes #2196

Closed
@blainemuri

Description

@blainemuri

Version

7.12.1 - 7.12.4 (haven't checked earlier versions)

Problem

Component where the return method calls a function instead of directly returning a jsx component will fail proptypes validation, even if the proptypes are properly declared.

Example

Broken

const SomeComponent = (props) => {
  function renderComponent() {
    return <div>{props.name}</div>
  }

  return renderComponent();
}

SomeComponent.propTypes = {
  name: PropTypes.string
}

The following error will occur:
'name' is missing in props validation react/prop-types

Working Example
No errors occur in the following case

const SomeComponent = (props) => {
  function renderName() {
    return <div>{props.name}</div>
  }

  return (
    <div>{renderName()}</div>
  );
}

SomeComponent.propTypes = {
  name: PropTypes.string
}

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