-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
The same object being proxied has different references in scope of the same observable #3917
Comments
This is intentionally, to have behavior consistent throughout all APIs. If you want to share the same ref, just https://mobx.js.org/observable-state.html#available-annotations Please note that using observables in useEffect deps is an anti pattern, I recommend to set up a reaction inside a useEffect, and have MobX instead of React deal with the deps. |
Thanks for the answer, especially for the link. We were also thinking about converting an object into an observable, but it make the parent and child components know about each other's internals: parent component should wrap an object with
WDYT? |
yeah I think that should work.
…On Thu, Sep 5, 2024 at 12:01 AM Andres Kovalev ***@***.***> wrote:
Thanks for the answer, especially for the link
<https://mobx.js.org/react-integration.html#useeffect>.
We were also thinking about converting an object into an observable, but
it make the parent and child components know about each other's internals:
parent component should wrap an object with observable() only because
child puts this object into an observable state. Probably it's better to
wrap it on a child level and memoize:
const Child = ({ object }: Props) => {
const observableObject = useMemo(() => obervable({ object }), [object]);
...
}
WDYT?
—
Reply to this email directly, view it on GitHub
<#3917 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAN4NBBBX2LJHZBEBGDRQG3ZU57K5AVCNFSM6AAAAABNUYW3RCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMZQGIZDCNJZGE>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
When I assign the same const object as a value of the same observable - I get different reference every time:
I understand
store.reactiveField
andconstObject
are different objects (the first one is a proxy for the second one), but it still looks counterintuitive and error prone to get a new reference every time. For instance, when this proxy is used as a dependency for a react hook:Is it possible to preserve the reference in a case when the actual proxy target has the same reference?
If there is a reason for such behavior, is there any workaround for that? At the moment the only idea I have is to keep the copy of the original (non-proxied) value and perform deep assign manually:
Intended outcome:
I expect the same reference can be reused (since the underlying value has not changed):
Actual outcome:
Reference is different every time:
How to reproduce the issue:
Here is the example code to reproduce:
Versions
^6.13.1
The text was updated successfully, but these errors were encountered: