Closed thany closed 2 years ago
What is this use case?
We have several components that are named after a certain pattern, in our case their names end in Section
. These are meant to be used by Sitecore JSS. These "section" components are nothing but proxies of another component. The props to these components are passed in by JSS in a single prop called fields
.
We don't want any propTypes there, because it would mean doubling them up. The proxied component are already enriched with propTypes, so any checking is done there. It would also mean we'd have to do some trickery to avoid having to redefine all those props in a fields
shape.
In short, these "section" components do not need propTypes. They are completely useless.
And the same would go for the corresponding defaultProps.
In my experience with these kind of components, it's very valuable to hoist the propTypes from the wrapped component onto the wrapper component, so that they do trigger propType warnings, even when shallow rendered.
There's even a package for doing it easily: https://www.npmjs.com/package/hoist-non-react-statics
I would guess that even with that package, it would become even less useful to not write explicit propTypes in those proxy components. But eslint would still warn about them not being there.
Sure, but in the rare cases where you're doing this, you can use an eslint override comment.
They are not rare. It's one kind of component, but there are many of them.
You could also name them with a convention, and use eslint's "overrides" with a glob to impose the no-restricted-syntax
rule and configure it to block any assignment to .propTypes
.
Didn't know about that one. Thanks. I'll check it out.
I have a use-case where I need to forbid propTypes/defaultProps entirely, on certain files.
Forbidding proptypes entirely can be done in a bit of a hacky way:
Basically forbidding every known propType type. However, this doesn't forbid the presence of propTypes alltogether. It also gives a misleading message when it is encountered as an linting error.
There also doesn't seem to be a way to forbid defaultProps entirely. Am I missing something?