-
Notifications
You must be signed in to change notification settings - Fork 47.4k
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
implications of shouldComponentUpdate on 'render callbacks' #4136
Comments
Another take on the third option: ...
<State initial={0}
extraProps={{ a: this.state.a }}>{
(val, set, extraProps) =>
<div id='B' onClick={() => set(val + 1)}>
clicked A {extraProps.a} times
clicked B {val} times
</div>
}</State>
... The difference is the third parameter that explicitly passes the data you say
When somebody has performance issues, they would be advised to turn the lambda into a class method. At this point they'll no longer have |
very nice, I like this better too. also gives a way to transition a dumb |
I don't think this is really a React issue. The discuss thread is a good place to keep talking about this. |
My bad about this, I've talked to @threepointone and suggested to create an issue for it. The initial goal is to turn this into something more actionable because it might require react-level support (assuming this pattern makes sense, I think it does). But I guess we can solve the problem fine without it. The same pattern @gaearon suggests can also work with scu with normal children passed to the wrapper. |
Sounds right. Thanks for the responses, folks. |
[for future readers] The goal of #4127 / #4136 was to document potential patterns/problems with the 'children as a function' pattern. If you have any thoughts, please share on the react-discuss thread |
@threepointone just in case - these changes to my code let animations go smoothly without re-rendering content. So it is a combination of shouldComponentUpdate => false and moving things that should not change out of updated component. |
Super late followup, but I don't quite understand why in the original example:
I think when Am I missing something? Or is this trying to solve for the generic case where the render callback may not be an inline function? |
@scottcheng My understanding: @threepointone has shouldComponentUpdate always return true so that it does properly rerender but would prefer a more restrictive shouldComponentUpdate method such as by following one of the methods in the original bulleted list (but can't due to the inability to introspect closures). |
@spicyj thanks for the reply. Sorry I wasn't being clear enough -- I was assuming |
Sorry to resurrect a very old thread but I came across this issue from Google, trying to understand how to make my render callback component more performant. I got very interested in @gaearon's suggestion, and had a go at implementing it. I've just published it to |
related to #4127 / https://discuss.reactjs.org/t/children-as-a-function-render-callbacks/626, this thread is to discuss and understand how
shouldComponentUpdate
should/could behave the render-callback pattern.Again, an example, using react-state -
In the above example, when
#B
is clicked, everything updates as expected. However, when#A
is clicked, and becauseState
doesn't receive new props, thenB
risks having stale data, and won't reflect the changes inA
's state. (This is technically correct, since the render callback here isn't 'pure')There are 3 methods (that I can think of) to work around this.
shouldComponentUpdate
to always returntrue
. This is my (current) preferred way, because the component is kinda equivalent to a stateful function wrapper on its 'children', and these children would have been rendered anyway. This is how react-state works (as also react-springs, etc)the con here is it's more fiddly. it also looks like ember's model dependency syntax, and that might not be too bad, the marking of explicit dependencies.
Thoughts / advice?
/cc @chenglou
The text was updated successfully, but these errors were encountered: