How to get memoized derivative array from an observable #3359
Unanswered
sebakerckhof
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, I just started out with mobx in react, and so far it's a delight compared to the state management systems that try to avoid mutable state.
I have a bunch of observable objects that form a tree structure (constructed from classes with
makeObservable
).Computed values that are members of the class work fine.
But now I want to derive a computed value, but not have it as a member of the class, since it's really just local state for the object (it builds an object that's used for an antd tree-select component).
So what's the best way to get this derived/computed value outside of the observable objects?
I found this works (rootNode being an observable object):
However, I find this quite verbose, is there a way to avoid this inner getter and just do something like this:
Unfortunately this doesn't work...
I also tried:
But this gets re-run everytime the component re-renders, even if nothing relevant in the tree has changed.
Wrapping this in a useMemo also seems to do the trick:
But now this is quite verbose again.
Is there a better way? If not, which one of the mentioned solutions would be preferred?
Beta Was this translation helpful? Give feedback.
All reactions