eslint-plugin-react: no-unused-prop-types errors when declaring an empty function as a default component prop

ESlint version 7.5.1

When adding an empty function as a default prop ESLint exits with the following error:

TypeError: Cannot read property 'properties' of undefined
    at markPropTypesAsUsed (/Users/asafdavid/homeis-dev/home-is-mobile/node_modules/eslint-plugin-react/lib/rules/no-unused-prop-types.js:656:38)
    at iterateProperties (/Users/asafdavid/homeis-dev/home-is-mobile/node_modules/eslint-plugin-react/lib/rules/no-unused-prop-types.js:825:15)
    at iterateProperties (/Users/asafdavid/homeis-dev/home-is-mobile/node_modules/eslint-plugin-react/lib/rules/no-unused-prop-types.js:361:11)
    at markPropTypesAsDeclared (/Users/asafdavid/homeis-dev/home-is-mobile/node_modules/eslint-plugin-react/lib/rules/no-unused-prop-types.js:810:11)
    at MemberExpression (/Users/asafdavid/homeis-dev/home-is-mobile/node_modules/eslint-plugin-react/lib/rules/no-unused-prop-types.js:1027:13)
    at listeners.(anonymous function).forEach.listener (/Users/asafdavid/homeis-dev/home-is-mobile/node_modules/eslint/lib/util/safe-emitter.js:47:58)
    at Array.forEach (native)
    at Object.emit (/Users/asafdavid/homeis-dev/home-is-mobile/node_modules/eslint/lib/util/safe-emitter.js:47:38)
    at NodeEventGenerator.applySelector (/Users/asafdavid/homeis-dev/home-is-mobile/node_modules/eslint/lib/util/node-event-generator.js:251:26)
    at NodeEventGenerator.applySelectors (/Users/asafdavid/homeis-dev/home-is-mobile/node_modules/eslint/lib/util/node-event-generator.js:280:22)

The default prop is declared as follows:

FeedWithSubheader.defaultProps = {
  componentRef: (() => {})
};

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 16 (10 by maintainers)

Most upvoted comments

Shouldn’t that be:

FeedWithSubheader.defaultProps = {
  componentRef: (() => {})
};

?

I came here because I experienced the issue, and noticed that I misspelled defaultProps as propTypes when I saw your comment. Renaming to defaultProps fixed it for me

@ljharb I found an old file, where () => {} was used as a prop type, instead of any. That was a reason of the problem for me

@AlexMarvelo any chance you could help narrow that down to which file/code is causing the error? One trick is to temporarily edit /Users/mac/dev/hitask-web/node_modules/eslint-plugin-react/lib/rules/no-unused-prop-types.js:656 and add console.log(context.getFilename()) above it (i might have the method name wrong)

The following test case works as well:

class MyComponent extends Component {
  render() {
    return <div>{this.props.foo()}</div>
  }
}
MyComponent.defaultProps = {
  foo: (() => {})
};

(Specifying defaultProps using the static syntax works as well.)

Maybe your lint error was caused by a more specific case?