-
Notifications
You must be signed in to change notification settings - Fork 47.5k
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
Are useEffect clean-ups called in the wrong order ? #17080
Comments
Can you provide a Code Sandbox with actual, runnable code that shows the unexpected behavior you're describing? Both effects and their cleanup functions are expected to run from the inside out- which is not the order your copy-pasted log shows, so it seems like some context is missing. For example, I would expect to see:
|
@bvaughn https://codesandbox.io/s/vigilant-feynman-0jxwb?fontsize=14 I also added the link in the original post. |
This comment has been minimized.
This comment has been minimized.
I also have similar issue with order of useEffect calls and I thought that non guarateed order of useEffect calls is intentional. |
Is there a difference from the |
Ok, I took a second look at my expectations, and I think everything is working as expected.
With such a layout, it makes sense that, when The OP should make a I'd say this is okay to close. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution. |
Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you! |
What is the current behavior?
I'm trying to write a small
Title
component to render nested document titles. However the kinda unexpected order in which effect clean-up functions are called makes it impossible to do this :Here is a loosy sketch of my component structure (handled with
@reach/router
) :Each one of these components calls my
Title
component. Everything works fine when going from profile to dashboard or from dashboard to project. But when I jump from calendar to dashboard, the document title gets messed up because the clean-up in Project is called before the clean-up in Calendar even though Calendar is a child of Project, which is kinda unexpected and makes tree side-effect logic using (just)useEffect
simply impossible.The order in which the effect functions are called is a direct reflection of the order in which the components are nested. Shouldn't the clean-up functions be called following the exact reverse order ? Isn't that one of the goals of a clean-up ?
When I navigate to the calendar and then back to the dashboard, I get the following logs :
codesandbox here
What is the expected behavior?
Here is what I expected and what I would get if clean-ups in children were to be called before clean-ups in parents :
I'm using React 16.10.2.
This ugly workaround works by "forcing" the order of what the clean-ups do (by keeping manually track of the effect functions' order and then just going the other way) :
The text was updated successfully, but these errors were encountered: