-
-
Notifications
You must be signed in to change notification settings - Fork 482
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
Should the Components really extend PureComponent? #150
Comments
@edorivai You can pass this external value as a prop to wrapped You could also use |
Yeah, nice hack. I guess that would work.
I understand what you're saying, though I also agree with Ryan's standpoint, that Moreover; as soon as someone passes the render function inline, this completely nullifies the effect of On the other hand, I agree it would be good to enable consumers of this lib to control whether
Not sure if I agree on this, as it could lead to confusing behavior if you're not 100% aware of what you're doing. Perhaps it could be preferred to have the more predictable, but less performant behavior of |
@edorivai good point with inlined render function, indeed PureComponent is always slower than Component without any benefit in this case. I think you started a good discussion :) |
For what it's worth, I asked around on twitter. Seems like it's really preferred not to extend Thread: https://twitter.com/EdoRivai/status/961930580702253056 Also, as quoted from the official React docs:
|
Okay. You've made a convincing argument. 👍 |
Published fix in |
I struggled a lot of time on the optimization of a large form, It should be stipulated somewhere in the doc that it is better to declare render render outside the render method, in a component or a instance method. https://reactjs.org/docs/render-props.html#caveats Also, because subscription props are (often) created in the render method, they always have a different reference and never pass the shallowEqual. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Hey, really awesome library, thanks so much for sharing!
tl;dr
I think that all Components that use render props should extend
Component
, instead ofPureComponent
.Longer version
My colleague noticed that the
<Form>
component is aPureComponent
, so he thought it'd be a good idea to maintain referential identity between renders, and define the render function in such a way that it would allow<Form>
to opt-out of rerendering. Like so:This works fine, as long as
renderForm
is a pure function, solely depending on it's own parameters. However, as soon as you mix in state or props from the wrapping component (Form2
in the example), therenderForm
function passed to<Form>
will become stale.This is all much better explained in the excellent article by @ryanflorence: https://cdb.reacttraining.com/react-inline-functions-and-performance-bdff784f5578
I think the quote most relevant for this issue is the following:
As I understand it, if you use render props, you should never extend
PureComponent
, since you don't know what's going on inside the render function.What are your thoughts?
The text was updated successfully, but these errors were encountered: