Skip to content

Using lodash get/set on models within observer() React components #1914

Answered by EmilTholin
kcoop asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @kcoop!

I think this should work in MST as well, but values returned and used from an action will not be observed. You can make getPathValue into a view instead:

export const Pathable = types
  .model("Pathable", {})
  .views((self: object) => ({
    getPathValue(keyPath: string): unknown {
      return get(self, keyPath);
    }
  }))
  .actions((self: object) => ({
    setPathValue(keyPath: string, value: unknown) {
      set(self, keyPath, value);
    }
  }));

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by kcoop
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants