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

Suggestion: Rule to disallow use of defaultProps for function components #3911

Open
cmorten opened this issue Mar 24, 2025 · 2 comments
Open

Comments

@cmorten
Copy link

cmorten commented Mar 24, 2025

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:

@cmorten
Copy link
Author

cmorten commented Mar 24, 2025

Options currently in community:

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.

@ljharb
Copy link
Member

ljharb commented Mar 24, 2025

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.

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

No branches or pull requests

2 participants