Skip to content

Using UndoManager as a volatile state #1870

Answered by EmilTholin
Guy-Michael asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @Guy-Rahamim!

Like you noticed the history returned from the volatile block is not an initialized model, you have to create it yourself.

Example

import { observer } from "mobx-react-lite";
import { types } from "mobx-state-tree";
import { UndoManager } from "mst-middlewares";

const Person = types
  .model({
    name: types.string,
    age: types.number
  })
  .volatile((self) => ({
    undoManager: UndoManager.create({}, { targetStore: self })
  }))
  .views((self) => ({
    get canUndo() {
      return self.undoManager.canUndo;
    },
    get canRedo() {
      return self.undoManager.canRedo;
    }
  }))
  .actions((self) => {
    return {
      setName(name) {
        self.name = name;

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Guy-Michael
Comment options

Answer selected by Guy-Michael
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