Skip to content

A useful type: map entities to properties #2267

Answered by OnkelTem
OnkelTem asked this question in Ideas
Discussion options

You must be logged in to vote

Ok, I investigated this further a little and this is what I got.

MobX supports objects as keys. E.g. this is ok:

const sugar = observable.object({
  servings: observable.map<Unit, number>([
    [{ id: 1, name: "tea spoon" }, 5],
    [{ id: 2, name: "table spoon" }, 10],
  ])
});

so we index servings map with unit objects.

We could also do this:

const sugar = makeAutoObservable({
  servings: new Map<Unit, number>([
    [{ id: 1, name: "tea spoon" }, 5],
    [{ id: 2, name: "table spoon" }, 10],
  ])
});

Looks like sugar.servings becomes an indirect observable.map.

However, when it comes to mobx-state-tree, we cannot use objects to index maps:

const productModel = types
  .model({
    servings

Replies: 1 comment 7 replies

Comment options

You must be logged in to vote
7 replies
@OnkelTem
Comment options

@coolsoftwaretyler
Comment options

@OnkelTem
Comment options

@OnkelTem
Comment options

Answer selected by coolsoftwaretyler
@coolsoftwaretyler
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
2 participants