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

State change triggered from callbacks do not notify observers... under some circumstances? #3738

Open
jaekwak02 opened this issue Jul 30, 2023 · 1 comment
Labels

Comments

@jaekwak02
Copy link

I might have found a bug, but I may also just be doing something wrong. I can't figure this behavior out.

Intended outcome:

Basically...

  • in the constructor, I am setting up a listener on the browser history from the history package.
  • I call navigateV2 on a button click.
  • The listener runs. It changes the state.
  • Observer components should rerender.

image

Actual outcome

Any calls to setRoute inside of that listener don't work. I tried:

  • calling it directly
  • running it inside of runInAction
  • calling setRoute inside of a setTimeout inside of the listener with varying delays.
  • wrapping the whole listener with action
  • setting the state directly rather than from setRoute. And changing individual keys of the current object, one at a time.

Additionally...

  • The listener DEFNITELY runs, calls setRoute, and changes the state. I confirmed that using console logs.
  • My components are DEFINITELY wrapped in observer and they run from any other state change. If I uncomment line 177, they update from that. Just not from any changes made within the listener.

Shouldn't this just work? If I've already confirmed that I set up the observer components correctly and they are rerendering from any other state change, I'm not sure why this, specifically, wouldn't work. It's not on all callbacks in my application, either, just this one.

I even see that setRoute is being run inside of MobX devtools:

image

How to reproduce the issue:

I'm sorry, I'm not sure how to reproduce this. I tried to set up something similar in a fresh project with the same compiler settings, but it seems to work perfectly... I'm so confused. Is there something I could be doing elsewhere with mobx that interferes with this listener state change notifying observers??

Versions

"mobx": "^6.10.0",
"mobx-react": "^9.0.0",
@jaekwak02 jaekwak02 changed the title State change triggered from callbacks are ignored... under some circumstances? State change triggered from callbacks do not notify observers... under some circumstances? Jul 30, 2023
@jaekwak02
Copy link
Author

I found the workaround for my situation, which is to mount the listener on useEffect.

image

I tried to find a minimal reproducible example, but I'm sorry, I just couldn't find any scenario where this happened outside of the one place I encountered it.

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

No branches or pull requests

1 participant