-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
[New] Add no-pure-component-children rule #1975
base: master
Are you sure you want to change the base?
[New] Add no-pure-component-children rule #1975
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't make any sense to me. The thing that would cause rerenders is passing new children to the PureComponent - what the PureComponent itself renders is utterly irrelevant.
Specifically, the linked example talks about how This, imo, is not something that is lintable in any reliable way. |
Thanks for taking a look! I agree with you, what the PureComponent renders is irrelevant and passing new children to PureComponent is what would would cause re-renders. The key here is that children are always new unless they're primitives* or cached, so having a children prop on a PureComponent is a smell and could be at least warned on And yeah, it would be better to check that the calling code doesn't pass new children but I agree that that's probably not lintable. Checking out this search shows there's a ton of PureComponents out there that are basically worthless because of children props *I can update this to check the types, we shouldn't warn if the children proptype is a number or string |
Precisely because children could be memoized or primitives, is why i think this doesn't make sense as a lint rule. |
I'll update to allow primitives, which leaves just the possibility of memoized children react elements as your argument against it? Note also that I didn't add this to the recommended list since I did not expect it to universally apply |
The problem is that I think this rule will create confusion, by targeting the wrong thing. This is a problem that should be solved at runtime by looking for excessive renders - potentially by monkeypatching PureComponent to track when they happen. |
Yeah I agree it could be solved that way (though regressions could be an issue) I think this also makes sense, so I'd like hear what the rest of the community thinks 👍 |
A PureComponent that receives It's a big footgun which kinda defeats the shallow equality check on the props, but there are still valid uses... 🤔 |
59af733
to
865ed16
Compare
069314a
to
181c68f
Compare
380e32c
to
51d342b
Compare
Overview
This PR adds a new rule
no-pure-component-children
which checks that a component extendingReact.PureComponent
does not declarechildren
as a proptype.Motivation
Consider standard use of
children
:On every render,
<Child /> !== <Child />
soParent
props will fail equality checks even though they appear to be equal. This means that ifParent
is a component that extendsPureComponent
, it will be less performant than if it extended justComponent
since it will do the extra work to check props which will ultimately always be unequal.See facebook/react#8669 for more discussion