-
-
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
function-properties in observable objects are not enumerable #2629
Comments
We may change it so that enumerability is respected, but I can't give you a promise atm, it's being discussed/worked on. |
For the time being as a workaround try: const obj = {
text: "abc",
onMore: action(() => {})
}; Or const obj = observable({
text: "abc",
onMore: () => {},
}, {
onMore: false
}); |
Please note that spreading observable objects into components as props is an anti pattern: https://mobx.js.org/react-integration.html#tip-grab-values-from-objects-as-late-as-possible. The idiomatic way to pass the notification down is If you can't make |
It seems very unexpected that properties may suddenly be removed from an object when enumerating, especially since imo mobx' power has always been that it makes you interact with the observables like they are native object/maps/arrays etc (with a little magic when you wrap them into observers). I can see how it is an anti-pattern but I've been using it mostly in tests and stories to quickly create a set of observable values that I then spread into a react component. When I upgraded to MobX 6 many of these stories broke. There might be tons of more use-cases but I think most important is that its just very DX unfriendly . |
EDIT: This is mostly wrong, and applies just to MST, not plain old observed mobx object, and doesn't have anything to do with the property being enumerable, just is fixed by similar work to fix this issue Just in case it wasn't clear, upgrading to mobx 6 from mobx 5 and beginning to use proxies causes a regression in The Before we could pretty easily do something like this: class Image {
constructor() {
makeAutoObservable(this)
}
highlight() { ... }
}
class Paragraph {
constructor() {
makeAutoObservable(this)
}
highlight() { ... }
}
class Spacer {
constructor() {
makeAutoObservable(this)
}
}
// duck type on the incoming node type to highlight it if it can be highlighted
const maybeHighlight = (node: Image | Paragraph | Spacer) => {
if ('highlight' in node) {
node.highlight()
}
} But now, const maybeHighlight = (node: Image | Paragraph | Spacer) => {
if (node instanceof Image || node instanceof Paragraph) {
node.highlight()
}
} where you have to maintain these lists of |
Looks like #2641 might change some of this, but if I understand correctly, |
Note that class methods are never enumerable by default, unless you turn them into fields: https://www.typescriptlang.org/play?#code/MYGwhgzhAEAa0G8BQ1XQLYFMAuALA9gCYAUAlIkimgL5K1LD4B2E20AHtALzROYDucMpUYt8ITADoQ+AObEkAeQBGAK0zBsk2TkX8mABQBO+AA6Yj2AJ4ARTBGBGAlqez4jxWJNMm318wA00ABEWHhEwaRIUaIQ4lIy8gDkYQSESdBOTBxRSEA. But that isn't needed either for the Also note that |
Alas, I have lied, you are right @mweststrate! My bad! The Here's a code sandbox explaining the issue from the MST perspective: https://codesandbox.io/s/loving-benz-3czkp?file=/src/index.ts
The Also FWIW the |
6.1.0 has landed. Closing this issue now. |
Intended outcome:
function-properties in observable objects are expected to be enumerable
Actual outcome:
function-properties are gone when iterating an observable object
How to reproduce the issue:
I created this example: https://codesandbox.io/s/jovial-black-rq0pw?file=/src/index.ts
The
onMore
-prop is actually not contained when callingObject.keys(x.notification)
since functions are not enumerable in observable objects. I am coming from mobx 4 and this used to work and now it breaks my app in some cases, since I use spreading to pass observable-objects to components like<Component {...notification} />
Versions
Mobx 6
The text was updated successfully, but these errors were encountered: