Skip to content

Correct way to store editable model #1679

Answered by EmilTholin
R-iskey asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @R-iskey!

One way of going about it is to use a reference to the model you are editing instead of cloning it. You can then store a snapshot of the model just as you start to edit it, and revert the model back to this snapshot if you cancel the edit.

Example

import { observer } from "mobx-react-lite";
import {
  applySnapshot,
  getSnapshot,
  Instance,
  SnapshotIn,
  types
} from "mobx-state-tree";

const ActivityModel = types
  .model("ActivityModel", {
    id: types.identifier,
    room: types.string,
    namespace: types.string
  })
  .actions((self) => ({
    setRoom(room: string) {
      self.room = room;
    }
  }));
type ActivityModelInstance = Instance<typeof ActivityModel>;
t…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@R-iskey
Comment options

@EmilTholin
Comment options

Answer selected by R-iskey
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