Why doesn't MobX go full-on proxy (so that any newly added properties are automatically observable)? #2683
Replies: 2 comments 2 replies
-
Eh MobX already does that. What made you conclude it doesnt?
…On Mon, 21 Dec 2020, 00:45 IKoshelev, ***@***.***> wrote:
For a few years now I've been waiting for a state-management library that
would utilize Proxies to give us state that looks like Plain Old JS
Objects, but is fully observable. I was excited to see that MobX went the
Proxies route after V4 (the one I'm using now), but after trying it out, I
feel like more could be achieved with Proxies.
I spent a weekend to try out a Proof-of-concept and was quite successful:
https://github.com/IKoshelev/react-mobx-mvvm-showcase/tree/observable-proxy-proof-of-concept
I was able to start proxying app state at the root, and make sure that all
the state is behind proxies (i.e. one Proxy per object). So, our comes down
to making a new state root:
export const app = makeApp({});
app.state.todos = []; //despite being added later, these are fully observableapp.state.filter = "ALL";
ReactDOM.render(
<div>
<AppContext.Provider value={{ app }}>
<TodoListCmp appState={app.state} />
</AppContext.Provider>
</div>,
document.getElementById('root'));
And ALL the state inside is granularly observable (you don't need to ever
explicitly make observable anything you add to tree). End result, despite
being a very bare-bones experience, looks quite sturdy. In fact, the result
is so good I'm surprised we don't have a mainstream state-management lib
that would take this approach.
So I was wondering, am I missing something? Does the "Proxy all state"
approach have some inherent flaw that makes it a bad idea?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#2683>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAN4NBB4HSVUVXRQEACMQNTSV2LELANCNFSM4VDQQWDQ>
.
|
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
danielkcz
-
MobX does this for |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
For a few years now I've been waiting for a state-management library that would utilize Proxies to give us state that looks like Plain Old JS Objects, but is fully observable. I was excited to see that MobX went the Proxies route after V4 (the one I'm using now), but after trying it out, I feel like more could be achieved with Proxies.
I spent a weekend to try out a Proof-of-concept and was quite successful: https://github.com/IKoshelev/react-mobx-mvvm-showcase/tree/observable-proxy-proof-of-concept
Hosted build: https://observableproxyshowcase.z33.web.core.windows.net/
I was able to start proxying app state at the root, and make sure that all the state is behind proxies (i.e. one Proxy per object). So, our app comes down to making a new state root:
And ALL the state inside is granularly observable (you don't need to ever explicitly make observable anything you add to tree). You can literally drop the following code in the console and it will work:
End result, despite being a very bare-bones experience, looks quite sturdy. In fact, the result is so good I'm surprised we don't have a mainstream state-management lib that would take this approach (a few similar experiments I found are mostly obscure).
So I was wondering, am I missing something? Does the "Proxy all state" approach have some inherent flaw that makes it a bad idea?
Beta Was this translation helpful? Give feedback.
All reactions