-
Notifications
You must be signed in to change notification settings - Fork 0
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
How far does observable(new MyClass()) reach? #5
Comments
Observable will create a proxy for the object passed to it. It cascades to all properties of the object and replaces any that refer to other objects with a proxy. The proxy itself will monitor mutations and if an unproxied object is stored in a proxied object it will create a proxy for it. It has special handling to extend this to maps, arrays and sets such that when they are mutated any objects being stored in them will be proxied. That is meant to cover all cases of extending observations to referenced properties (except for the arrow example in issue #6 which I will explain separately). Given that it is not clear to me why in your example this code would create a Todo that is not proxied and thus not cause re-rendering when the todo is edited:
I took the code you provided as a guide to create a test to see if I could find out why but could not reproduce that issue.
This particular todo did cause a re-render when edited at least in the way that this test edits it. Would you be able to provide a codesandbox to reproduce that issue. |
Coming from Mobx, we have a pattern of having nested stores that are observables and passing them via props.
Prefer that over using providers/hooks as that's a contract a component has in its type signature.
The exception to that rule is Themes, but that's not important.
All components define what they need to be passed to in order to be constructed.
The stores could look like this (prefer class based approach)
It seems like from testing.
I only need to do
const store = observable(new ApplicationStore())
However, I did notice that in the constructors of the various nested stores, like for instance
That particular Todo isn't observable, meaning editing its text, does not trigger re-renders.
Could you explain how
observable()
works? Does it go deep and make everything observable on an instance and its fields, and the future objects added to its lists?From testing, creating and adding a new
Todo
to the map insideTodoStore
automatically reflects in the list.(todos is a memoized
Array.from(this._todos.values())
)But also editing the
Todo
within that component will reflect as well, and work just like mobx does with memo, in that only the component that edits will render and nothing else. It's impressive how close to Mobx you've gotten in terms of dev UX but also supporting the new concurrency rules.The text was updated successfully, but these errors were encountered: