Closed seivan closed 4 years ago
Explained in https://github.com/mobxjs/mobx-react/issues/848#issuecomment-604925431
Thank you @mweststrate
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs or questions.
Why is the body of a defined component triggered after an update to
person.name
when usinguseObserver
but not<Observer>
?useObserver
this will logOutside useObserver
everytimeperson.name
is set, doesn't have to be different. Setting the same value will trigger theconsole.log
in the body.<Observer>
this will only logOutside <Observer>
a single time, and never again even ifperson.name
changes.Why do they behave differently, and when approach is preferred?
My theory is that the body is triggered when reconciliation runs, which is why it's run every time
person.name
is set, and not necessarily changed.This is hidden from me since it might just run for the
<Observer>
component, but not my wrapping component since it's not an actual observer.While
useObservable
hook makes my component an actual observer, thus its body will be triggered for each change.But I'm not 100% sure if it's accurate or just a bug?
You can try this in the sandbox but I also changed
mobx-react
andmobx
to the latest versions.