Skip to content
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

componentDidUpdate affects component state #3742

Closed
1 task done
zjhch123 opened this issue Sep 22, 2022 · 2 comments · Fixed by #3743
Closed
1 task done

componentDidUpdate affects component state #3742

zjhch123 opened this issue Sep 22, 2022 · 2 comments · Fixed by #3743

Comments

@zjhch123
Copy link

  • Check if updating to the latest Preact version resolves the issue

Describe the bug

componentDidUpdate affects component state

To Reproduce

https://codesandbox.io/s/crimson-worker-1koisq?file=/src/index.js

  1. Open Console, it shows { count: null, isLoading: false }
    image

  2. Comment the line of componentDidUpdate, refresh demo page, in console, it shows { count: { value: 10 }, isLoading: false }
    image

Expected behavior

Whether or not there is a componentDidUpdate event, the console should be { count: { value: 10 }, isLoading: false }

@JoviDeCroock
Copy link
Member

Just took a quick look at the issue and it looks like the following is happening:

  • componentDidMount gets invoked and initiates a render.
  • we push a componentDidUpdate to the _renderCallbacks
  • componentDidMount gets invoked and updates state
  • the above setState call pushes to the _renderCallbacks queue
  • render completes and the renderCallbacks get consumed before the next render is initiated making the setState callback called with a stale state as that one completes on the next invocation

@zjhch123
Copy link
Author

Thank you @JoviDeCroock for your quick action!

JoviDeCroock added a commit that referenced this issue Sep 23, 2022
JoviDeCroock added a commit that referenced this issue Sep 29, 2022
JoviDeCroock added a commit that referenced this issue Sep 29, 2022
* create test case for #3742

* solve issue

* types

* golf

* add mangle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants