eslint-plugin-react icon indicating copy to clipboard operation
eslint-plugin-react copied to clipboard

Suggestion: Rule to disallow use of `defaultProps` for function components

Open cmorten opened this issue 8 months ago • 2 comments

React 19 sees the dropping of propTypes and defaultProps for function components.

The change to defaultProps is of particular interest because it has a runtime impact (ignoring any caveats/special cases for propTypes in this issue) - for like for like behaviour consumers are now forced to migrate to ES6 style default arguments (or alternatives, e.g. hoist the default to the parent component etc).

I would be interested to test appetite for either a new rule, or adjustments to existing rule(s), to allow consumers of this lint plugin to forbid usage of defaultProps on non-class components (assuming I’ve not overlooked existing support for this in some form).

Eventually I could see such a rule becoming redundant once sufficient time has passed such that the general React community has forgotten about defaultProps etc., but in the meantime such a rule would help to protect React community component authors from themselves where years of habit results in them reaching for the API which silently does nothing (*silent in production, with logged warnings/errors in development).

Because of this habit I suspect current themes of disabling certain rule capabilities (manually or automatically by detecting R19+) in other issues doesn’t quite cut it? Some form of opt-in (manual or automatically for R19+) behaviour to warn users that they’ve accidentally introduced defaultProps would be fab.

Related discussions:

  • https://github.com/jsx-eslint/eslint-plugin-react/issues/2856
  • https://github.com/jsx-eslint/eslint-plugin-react/issues/3753

cmorten avatar Mar 24 '25 07:03 cmorten

Options currently in community:

  • https://eslint-react.xyz/docs/rules/no-default-props
  • https://github.com/brettfarrow/eslint-plugin-react-19-upgrade?tab=readme-ov-file#supported-rules

One totally valid option is to suggest out of scope for this package and consumers reach for this other solutions. It would be personal (and maybe reflects other consumers of this package?) preference to avoid linter package proliferation by consolidating on just this package.

cmorten avatar Mar 24 '25 07:03 cmorten

It seems that the https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/prop-types.md rule should be updated to, when the react version is 19+, no longer accept propTypes on SFCs, and the https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/require-default-props.md rule should similarly, when the react version is 19+, no longer accept defaultProps on SFCs.

ljharb avatar Mar 24 '25 15:03 ljharb