ComponentDidMount prevents observer #3379
Replies: 7 comments 5 replies
-
@filipetedim Thanks for a detailed report. Am I right that you unable to reproduce this issue in codesandbox? |
Beta Was this translation helpful? Give feedback.
-
Is componentDidMount accidentally defined as arrow function in the real
case?
…On Wed, 27 Apr 2022, 20:04 Egor G, ***@***.***> wrote:
@filipetedim <https://github.com/filipetedim> Can you set up a simple
github repo with reproduction?
—
Reply to this email directly, view it on GitHub
<#3379 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAN4NBGDLEFB7F5YUZWHUKLVHGFVLANCNFSM5UPUOM2Q>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Have you tried production build? With any kind of HMR out of the picture? |
Beta Was this translation helpful? Give feedback.
-
I can confirm the exact same behavior. I kept running into issues with Mobx React components not updating, and finally tracked it down to this exact issue. When I remove componentDidMount (event a completely empty method), I can observe stores again. This is a monumental gotcha. Please fix it |
Beta Was this translation helpful? Give feedback.
-
Dunno why it happens in this case, but it could be a similar problem to: |
Beta Was this translation helpful? Give feedback.
-
Just released mobx-react@7.5.1, could you give it a try? |
Beta Was this translation helpful? Give feedback.
-
just remove <React.StrictMode> at the entry. |
Beta Was this translation helpful? Give feedback.
-
Context
I'm creating a greenfield project with CRA TypeScript and MobX 6. I've only added
mobx
andmobx-react
to it, along with the followingtsling.json
settings:yarn create react-app cra-test --template typescript
yarn add mobx mobx-react
I'm running on Windows 11, WSL 2 and VSCode with Hyper setup for WSL as terminal. My project folder resides within the linux subfolders under
\\wsl$\Ubuntu\home\$user\
as anywhere outside (meaning in mounted windows folders) was not allowing the hot reloading to work.Problem
For some reason, using observer on a class that has a
componentDidMount
prevents that class from observing.However removing the
componentDidMount
call correctly makes it update.Furthermore, the same code in this codeandsandbox I've built works correctly.
The only difference between that and my local CRA app is that it was initialized with CRA TypeScript which adds a little bit of boilerplate.
I've also noticed that my local logs "mounted" twice, whereas codeandsandbox only does once, unsure if it's affecting it or not.
As soon as I remove
componentDidMount
, it updates correctly.It also happens regardless of using a decorator or exporting as
observer(App)
Steps taken
I've already rebuilt and created twice, same problem. Also tested with components instead of the main
App.js
and the same happened.componentDidUpdate
seems to be working however.I believe it might be something I'm completely misunderstanding with MobX 6m, although I was under the impression MobX does not care much about the React lifecycle after some digging around.
Any ideas? Thanks!
Edit: I am aware I don't need to use class based components, but I still want to understand the reasoning as this prototype will be for a full refactor of our company's main app, where we have very specific bits of code that we need to run before rendering on some components. I'm also aware there are probably better approaches but when deep diving into a refactor like this I want to feel comfortable enough in understanding the specifics of the new tech we will move into.
Code
Store.js
App.js
Beta Was this translation helpful? Give feedback.
All reactions