-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
unimplement shouldComponentUpdate? #507
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
Comments
Oh boy, this oughta be good... :) |
Just to be clear and make sure everyone understands what's going on, here is the situation with something like react-redux and React Router: <Router>
<connect(App)>
<Link to="/foo" activeClassName="active"> In this case, let className
if (this.context.location == this.props.to)
className = this.props.activeClassName (That's a crazy over-simplification, but it gets the point across) If the URL/location on the page changes, then that information should propagate from the The problem lies with Hopefully that explains the situation well enough. How to fix it is another matter :) |
I think react-redux's use of sCU is fine. We've already got an issue open on the router (v4 branch) to stop using context to communicate state changes to |
So, I'm wondering if you could store the props for the Basically, you would change this line to store BTW, there's already a potential opt-in option: |
@timdorr I don't think changing where the computed props are stored would unblock things. The issue is that the implementation of |
Actually, that is correct, though it would be a bit more idiomatic React to store something in setState, rather than use it as a workaround to forceUpdate. But sCU needs to be removed for it to stop interrupting context propagation. I don't think there would be any problem with doing that right now since the props are stored and precomputed by the selector. |
Does adding this to the 5.0 milestone mean that it will be done in 5.0? I'd like to see it for reasons unrelated to React Router: I keep the current user in |
@jimbolla How about we do this as a test release (5.0.0-test.1) and see what the effects the community can find? I'd also love to have a benchmark suite available so we can have an objective analysis on our own, but that could work in a pinch until we get something built out. |
I'm not opposed to that. But do we want to remove sCU completely or make it opt-in or opt-out? The code changes are easy either way, but tests will need tweaked and docs updated. |
I'd go with opt-in. Most users don't need it and it's an optimization that comes with side effects, so turning it on should be a conscious decision by the user. Plus, we can document those side effects to warn people about it ahead of time. |
Ok. That should be easy enough to implement. What should the option be called? |
Can it be combined/tacked-on to the |
Probably not. You usually want |
Would it make sense to pass predicate with the options how shouldComponentUpdate check for update? I would want one selector with many properties that changes or not depending on some index, to connect in with different "Value" components. One Value component would display only one value - property but it would be connected to the same memoized selector. something like:
Cheers |
@primozs I don't really understand your use case. When would you want a component that received changed props to not re-render? |
Depending on how #525 is resolved, we may need to just leave this alone, because it's possible the solution to that could require calling |
Based on the findings in #525, let's put this on a 5.1 release and wait for React 16 to push that out (so users can easily clamp down to the 5.0 if they can't upgrade from React 15 easily). |
Why not just compose->replace shouldComponentUpdate? |
It looks like the fix for #525 didn't affect whether we need to change our plans on this. So my opinion is we should just remove sCU completely... at least as the long term plan. The question being whether we offer a migration path in interim versions or not. Since removing sCU could have negative perf impact on someone relying on it to prevent unnecessary rerenders in their own components, I think we should treat its removal as a breaking change, which would mean it would land as version 6.0. I suggest we:
|
Just to recap and check my own understanding: edit Boy, I should really go back and re-read the start of the thread. Yes, that's apparently the case. |
The 4.x implementation relied more heavily on sCU because of its order of operations when a store change is made. With the new implementation's avoidance of setState unless it's really necessary, that's no longer the case. Now, sCU will always be true if it's firing in response to a store change. It can only ever be false if it's happening because of receiving new props from parent. |
Adhering with the updated react-redux API which [favors composition](reduxjs/react-redux#507)
Adhering with the updated react-redux API which [favors composition](reduxjs/react-redux#507)
Closing this out in favor of moving to the new context API. |
There was some good discussion on Twitter (between @gaearon, @markerikson, @timdorr, @mjackson, @ryanflorence, et al.) about
connect
's component implementingshouldComponentUpdate
.If I understand correctly, the main issue being that any component wrapped in
connect
then blocks updates related to context changes, and this has an adverse impact on libraries that pass data via context that need to trigger rerenders (such as React Router's Link component.)The changes in the
next
branch of React Redux make its implementation ofshouldComponentUpdate
much less necessary, mainly because nowsetState
is no longer called as a response to every store state change, but only if the final merged props have changed. So now whenshouldComponentUpdate
is called as a result of callingsetState
, it's always going to return true anyways. (The call to setState could probably be replaced with forceUpdate and would work exactly the same.)In the case of
shouldComponentUpdate
being called after receiving new props from parent, it's effectively just acting likePureComponent
. That responsibility can be given to the components being wrapped, which would have better knowledge about if/how they should implementshouldComponentUpdate
. I personally would use recompose and do something like:An alternative to removing connect's
shouldComponentUpdate
completely would be to make it an anotheroption
argument, and decide whether it should be opt-in or opt-out.The text was updated successfully, but these errors were encountered: