-
-
Notifications
You must be signed in to change notification settings - Fork 3.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
Experimental useSelector to avoid stale props #1505
Conversation
Deploy preview for react-redux-docs ready! Built with commit 719d97e |
Looking at this implementation, it's not going to work as-is, and I think it's going down the wrong path. In particular: subscription.onStateChange = dispatch We have to do the selection and diff checking work before React gets involved. This forces a dispatch as soon as the store is updated, regardless of whether this component's selected values have changed or not. |
That's true, but react bails out rendering if the selected state is not changed. Your concern (and mine too) is the performance. I'll try doing benchmarks in reactive-react-redux (just because I'm used to it). The whole point of this experimental change is we no longer require try-catch hack for stale props. We read the future selector and let useReducer bail out. It may slows down a bit for sure, but hopefully not noticeably. |
My intuition says this would kill perf. Every Redux-using component is going to be flagged for re-render, on every dispatch. |
Unfortunately, it's not like what I expected.
The result doesn't seem bad, but as it's not likely to solve the stale props issue and there would be many edge cases to be broken, let me close this. Would be glad to continue discussion and hope someone come up with better ideas... |
Just wanted to leave a comment here because I saw the discussion in facebook/react#17953 (comment), which is somewhat related to or at least inspiring my experience in this experiment. Experimenting further, the current behavior is that reducer is called twice, one with stale selector and the other with new selector. So, if we used a ref value we could control it. Long story short, it worked and revealed this approach has limitation in performance. Note: this is about blocking mode, not concurrent mode. |
719d97e this commit passes the test/integration/stale-props.spec.js test. It behaves like this:
We don't see the intermediate state in step 4, but it seems the render function with the hook actually runs (but doesn't commit). Please check out the code if someone is interested. Note that this experiment is mainly a trial to pass the stale-props spec. It means that the spec itself can be valuable (I'm not the original author). Lastly, although this approach has the performance limitation to solve stale props, it could be still useful. #1509 uses this approach to implement use-subscription technique. (It doesn't solve stale props, but CM tearing.) |
@markerikson said https://gist.github.com/KidkArolis/26cb3f998899b525dbdfd85f7f3c7978#gistcomment-3214066
Well, yeah... but v6 didn't have Hm, this is far slower than I expected. I wonder how the benchmarks are going to go with
|
https://twitter.com/dai_shi/status/1219245681249939456
I just noticed we haven't tried this, have we?
stale props spec is from #1179 (comment) by @MrWolfZ.
Unfortunately, useSelector.spec is failing. Although I spent some time, I'm not sure how this is happening. react-redux's Subscription mechanism is still new to me.