-
-
Notifications
You must be signed in to change notification settings - Fork 72
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
Is it different from MobX? #7
Comments
Well, MobX is definitely broader and can be used anywhere, not just with React. My comment was certainly somewhat far fetched for sure. You are right that you don't change the way how the state is mutated. MobX does that. Also, you don't have a concept of Your project definitely has its appeal for people who have code base with Redux (or similar) and want to optimize renders, but I don't think that mixing with MobX would bring any benefit. On contrary, it might be causing double re-render if used together. |
Thanks for your comment! I think conceptually the observer pattern and this project can be combined. Maybe, it could be done with MobX core without react binding, but still not trivial (I mean react-tracked would need be modified for subscription.)
Yeah, that's what I expect. Probably, not because of technical issues, but mental model.
That's what I thought too, if it's done naively. |
It reminds me of one other comment: |
I think I was wrong. The notion of "Computed values" in MobX core might not fit well with useTrackedState. (edit) Hmm, maybe not? I think I'd try to create an example. |
Yea, computed observables are a very powerful feature, I wouldn't want to live what that anymore :) Personally, I love to use https://github.com/immerjs/immer when I need to tackle mutations to immutable state. Perhaps it would make sense for this project to join forces there. The Immer is from the same author as MobX and there are some rumors it might work much better in Concurrent React, at least for a local component state. But generally, I do avoid reducer pattern, it's just too convoluted for my taste. |
Yeah, I'm sure immer works nicely with react-tracked and reactive-react-redux.
Hey, I have a question on this. Doesn't MobX update state immutably? |
Um, quite opposite :) MobX always mutates and lets listeners know about the change. I mean it mutates primitive values, but objects, arrays are immutable. It's what I like about MobX the most, I don't need to think in terms of sCU, just use whatever I need to render and it will work. |
Oookay, I got that. It makes sense. I wonder how MobX/react works with React.memo then. Maybe not recommended? I have another finding. (edit) That applies to |
Finally, created an example to combine mobx-react-lite and react-tracked. It's very tricky, and not meant to be a real thing. But, the idea is that while |
Uh...wow? :) You know, we were kinda fighting initially with this problem of needing observer around what's being rendered to keep track of it. What's the most surprising that it doesn't actually re-render more than necessary. Just for a convenience a modified version which logs renderings. https://codesandbox.io/s/peaceful-swartz-cn280 @mweststrate Sorry, but I have to ping you in here, you have to see this :) ☝️ |
Super cool project!
I'd have to tinker about this a bit more, but yes, I think this could
potentially be useful for a completely alternative api to observer.
The problem I was running into so from being able to say just
`useObserver()`, is that we could already implement starting tracking for
the current component, we just didn't have a means to reliably _end_
tracking for the current component. (Not fully sure that is unsolvable, but
didn't see a solution yet).
So this solution to create a proxy (if I understood it correctly from the
source) per render, so that we every read an be traced back to the owning
component solves that problem (granted, one has to has proxies).
…On Mon, Aug 5, 2019 at 7:06 PM Daniel K. ***@***.***> wrote:
Uh...wow? :) You know, we were kinda fighting initially with this
*problem* of needing observer around what's being rendered to keep track
of it. What's the most surprising that it doesn't actually re-render more
than necessary. Just for a convenience a modified version which logs
renderings.
https://codesandbox.io/s/peaceful-swartz-cn280
@mweststrate <https://github.com/mweststrate> Sorry, but I have to ping
you in here, you have to see this :) ☝️
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#7?email_source=notifications&email_token=AAN4NBHF6BRNVGF7JG6NFMDQDBMYVA5CNFSM4IJLUHAKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD3SOL6Q#issuecomment-518317562>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAN4NBD6BWTYEOIQGDJZQQTQDBMYVANCNFSM4IJLUHAA>
.
|
Yeah.
We can do the same with object getters, no? |
https://codesandbox.io/s/blissful-breeze-jnsbo |
I wouldn't see it as a bad thing. On the contrary for people who are used to |
Agreed.
Not sure if I understand this. Anyway, out of my curiosity, I made another example. const Counter1 = () => {
const store = useObserver();
console.log("render Counter1");
return (
<div>
{store.count1}
<button onClick={() => { store.count1 += 1; }}>increment1</button>
{Math.random()}
</div>
);
}; This requires me to modify react-tracked a bit. branch This is not meant to be a real thing (nested objects may not work), but I hope you get the idea. Look forward to your comments. @FredyC @mweststrate |
Well, in your initial implementation you were using And this is also my concern. The Nonetheless, your latest attempt certainly has an appeal. It's kinda funny because we were attempting a somewhat similar approach by utilizing babel-plugin-macros (rather awkward in retrospective). It's a nice surprise that something like that is possible in runtime. I think that if we are going to pursue this direction, it would probably make much more sense to implement it natively without dependency on |
Yeah, it wouldn't require triple proxies. 😄
I see. It makes sense. It's possible because
react-tracked is so tied to react local state and the state is updated immutably. So, I can't simply use the bare Provider/useContext which would trigger all consumer components to re-render. By the way, I extracted the context part (no tracking support) from react-tracked and created another library. Another reason I use own Provider (special context) is for concurrent mode. Surely, subscriptions work in concurrent mode with the technique like This leads to my experiment. |
Makes sense. That's what so great about MobX, because it has stores with stable reference, the Provider won't even re-render in most cases. Only components accessing variables that have actually changed will render. Recently, I wanted to use Formik V2, but there are so many issues related to immutability and that everything is recreated on each keystroke. I ended up writing my own form solution based on MobX and it's awesome. It's not for public consumption though (yet). I even wrote an article about it some time ago ... https://levelup.gitconnected.com/formik-with-react-hooks-and-mobx-1493b5fd607e That selector hook is definitely useful for immutable state, but in case of MobX not so much :) About your experiment, that's something that I am afraid of too even for MobX, because |
Hi @FredyC
I would like to compare Mobx performance with Hookstate performance using this benchmark. I wonder if you could advise me how Mobx store should be used correctly, so only the single table cell is rerendered when it is set, but not the whole table? |
Also, @FredyC , I have noticed you had problems with large formik forms and Mobx rerendering whole form on each keystroke. Here is the solution this this problem using Hookstate: https://hookstate.netlify.com/local-multiple-consumers |
@avkonst This is hardly a place to get help with MobX :) Please open issue at https://github.com/mobxjs/mobx-react |
I just found a comment by @FredyC in facebook/react#15156 (comment).
I'm not very familiar with MobX, so please correct me if I'm wrong.
As far as I understand, MobX uses Proxies to trap object mutations, not object property access in render function. I mean, what it's providing is not an alternative to Redux/ReactHooks reducers.
Theoretically, MobX is complementary to react-tracked. I'm not yet sure if/how it can be implemented, though.
react-tracked doesn't change the semantics of render function. It tracks the state usage and trigger re-renders. How to update state is not the focus of the library (at least at the moment).
I wrote an example to show the power of state usage tracking in the "Advanced Example" in https://blog.axlight.com/posts/effortless-render-optimization-with-state-usage-tracking-with-react-hooks/.
(edit) There was a misunderstanding of mine.
useObserver
tracks the state usage.The text was updated successfully, but these errors were encountered: