-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
fix(hooks): flush useEffect hooks with a rerender #216
Conversation
3591cb0
to
3b3bd8f
Compare
👍Cool, what about the case with a timer inside a hook? |
Yeah, I'm afraid this wont quite work for many common cases: https://codesandbox.io/s/vqlm09qyx5 I wonder if there's something we can do with the new scheduler package 🤔 |
Not sure, I dug around in |
Ok, I did some digging: https://www.youtube.com/watch?v=JQeB9miT9Wc Conclusion: this wont be enough. I hope we can come up with a better automatic solution to the problem. If we cannot then we'll have to export a It's not a terrible solution, but I'd love to find something more automatic if we can... I wonder if there's a way for us to hook into the scheduler to know when things are scheduled and just make everything run synchronously... Seems dangerous. I'd rather have an officially supported API from React for this... Thoughts? |
I'd say yes to the last part. Can't you reach out to someone from the React core team, privately, over twitter or in this very thread or that of the issue? Update: seem Dan Abramov already agreed there should be an api to trigger them. |
It's great there will be an API, but why don't they work automatically? Tests shouldn't have to know when an effect is supposed to be fired and manually call them, that's super brittle and not how the code runs in prod. As an additional workaround, could we call another render after fireEvent? |
Another hacky solution could be to mock React.useEffect to point at React.useLayoutEffect. |
I agree with you. Although, if there's an api that the test library (this library in this case) can call internally, it's not that big of a deal. The worse scenario would be if this library ends up having to take this responsibility to the code using it. That's what I'd try to avoid the most. |
Yeah @alexkrolick, those occurred to me as well. I'm going to reach out to some folks and ask them what they think. |
Million dollar question. Surely I'm missing something, but since we're using |
I don't think this is the direction we should go. At least not right away. Let's wait for a while before we try to solve this problem with something so opinionated. |
…yver-patch-1 fix: export typings
Fixes #215
This change just calls ReactDOM.render twice in a row to trigger the effects from the first render. It seems like there are cases with subsequent async actions where this could cause unexpected behavior.
EDIT: The second commit changes the approach:
Instead of rendering the same element again, render somewhere else in the document. The second render target doesn't have to be in the
body
and it seems to trigger the effect.