-
-
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
extendsObservable doesnt trigger re-render #1038
Comments
ExtendsObservable can add observable properties just fine. But the addition of these properties itself is not an observable fact, so if you need to react to the fact that a property is added itself, then you need to use maps indeed: const a = observable({
x: 1
})
autorun(() => {
console.log(a.x, a.y) // won't react to a.y initially, as it is not an observable property
})
extendObservable(a, { y: 2 }) // won't trigger autorun, the creation of a new observable property is not something tracked by the autorun
a.x = 2 // this will trigger the autorun as usual
a.y = 3 // but this as well! in the last run (caused by the change in a.x) a.y was an existing observable propery of a, so mobx started tracking it Maps don't suffer from that problem, as they have explicit support of tracking not-yet existing fields. Note that the above could also be fixed by simply: const a = observable({
x: 1,
y: undefined
}) |
Is it possible that a new added property via extendOberservable will be overserved?
? Maybe that could be implemented in a new version :)
|
No, it's technical limitation. MobX uses getters and setters to perform subscription/notification. MobX doesn't know if that not yet defined property was accessed in some reaction/autorun, because the MobX getter was not invoked, therefore it can't notify that reaction/autorun to re-run. const handler = {
get(target, key, proxy) {
if (!isObservable(target, key)) {
extendObservable(target, { [key]: undefined });
}
return target[key];
}
}
// EDIT: note it won't be enought if you're just iterating over keys
// you may need to implement "ownKeys" and "has" handler and it still may not be reliable enought However be aware of possible performance problems, not with proxy, but with |
See #652 |
@mweststrate I'm wondering if there's some kind of hack to trigger a force render after |
Yeah, change one of the already existing properties :-P. But no, really,
either use maps, or declare your properties upfront.
Op wo 23 aug. 2017 om 11:08 schreef Iftach Yakar <[email protected]>:
… @mweststrate <https://github.com/mweststrate> I'm wondering if there's
some kind of hack to trigger a force render after extendObservable.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1038 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ABvGhGSYgIwwbt3fbt7Ez8l7MOiSEm5Dks5sa-v4gaJpZM4N06Ta>
.
|
of course, re-reading the docs "after" reading this issue - it makes much more sense (i can close my stackoverflow question now). i think this page needs a little more regarding "late-add" of properties. I don't mind declaring them upfront, i think that is a win.. it's just not clearly defined in the docs (or i missed it) |
Feel free to PR doc improvement!
Op wo 24 jan. 2018 14:39 schreef Paul Stanton <[email protected]>:
… of course, re-reading the docs "after" reading this issue - it makes much
more sense (i can close my stackoverflow question now). i think this page
<https://mobx.js.org/refguide/object.html> needs a little more regarding
"late-add" of properties. I don't mind declaring them upfront, i think that
is a win.. it's just not clearly defined in the docs (or i missed it)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1038 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ABvGhJ8ik3rLjSUfUYlG2uLWhG2MR5ZHks5tNzEPgaJpZM4N06Ta>
.
|
So, I understand why we can't detect direct access to a yet-to-be-defined property, but is there no way to have
Oh...maybe not, because |
|
Hello everybody,
I known there are a lot of closed tickets with the same topic.
(See: #194)
All tickets got closed with the answer: Use observable.map!
But why? The documentation says for "Observable Objects":
Sure I could use observable.map but there I dont have the nice dot notation for accessing properties in javascript. And this is what I want, because this is my code convention.
My question now is: Do the documentation tells wrong information or is there a way to get extendsObservable work?
The text was updated successfully, but these errors were encountered: