-
Notifications
You must be signed in to change notification settings - Fork 103
Conversation
@sophiebits can you please take a look and see if I'm missing anything here? |
Unfortunately this looks incorrect. You assign to mapStateRef in the render phase but read from it in the commit phase. That means if we start processing an update to mapState but it hasn’t committed yet, your check function will use the new mapState but it should use the old one. |
Instead, use a ref to hold the last `mapState` value both for comparision and for acess within the store callback.
d879d70
to
3c9738c
Compare
@sophiebits is it possible to write a test for this case? Sounds like the solution is to use |
Actually, does it matter if the check uses the new value of |
Ah, I get it now! Thanks for the explanation, @sophiebits. I'm wondering though, if |
I've been looking at this PR for over an hour. Maybe it's my misunderstanding of hooks - but I can't see the issue with using a |
@ianobermiller If the new map function returns a value that happens to be the same as |
@sebmarkbage if it is returning the same value as before, it is expected that the component doesn’t rerender, even if the mapState function changed. I’m still struggling to see the correctness issue here. |
The new map function hasn’t happened yet though. That’s a future state. So in the current state’s map function it should rerender. |
Are you saying that the |
8cab489
to
9581ce9
Compare
I think I finally understand this, here is my attempt to explain it. With concurrent rendering, the render phase can happen without the commit phase, and then might happen again before it actually commits. For a functional component, render is just calling the component. The blog post on the new lifecycle methods explains this well, calling it "async rendering". Though it doesn't talk about hooks, it makes the distinction between "render" lifecycle methods like For hooks, the only ones that run during commit phase are the callbacks to
Now, what makes this implausible to me is that in step 3, not only does it set the ref, but it also calls |
According to https://reactjs.org/docs/hooks-faq.html#how-do-i-implement-getderivedstatefromprops, I assumed the set state call at render time would cause the component to immediately rerender again. |
I Dan's latest blog post, he's using a |
Superseded by #40 |
Instead, use a ref to hold the last
mapState
value both for comparison and for access within the store callback.