Skip to content

Composite Key/Identifier - Get #1762

Answered by jamonholmgren
mashaalmemon asked this question in Q&A
Discussion options

You must be logged in to vote

To be honest, my approach would probably be a lot less fancy. I'd just preprocess the snapshot

import { types } from "mobx-state-tree";

const User = types
  .model({
    name: types.string,
    uuid: types.string,
    id: types.optional(types.identifier, "")
  })
  .preProcessSnapshot((snapshot) => {
    const id = `${snapshot.name}::${snapshot.uuid}`;
    return { ...snapshot, id };
  });

const UserStore = types.model({
  users: types.array(User),
  currentUser: types.reference(User)
});

const myUser = User.create({ name: "Jamon", uuid: "sadf" });
const userStore = UserStore.create({
  users: [myUser],
  currentUser: myUser.id
});

Sandbox here: https://codesandbox.io/s/angry-kepler-fu…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@mashaalmemon
Comment options

Answer selected by jamonholmgren
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