Resetting the undo/redo history of a Slate.js editor

Josh Sherman
1 min read
Software Development JavaScript TypeScript

Slate.js has some wonderful undo/redo history under the hood, by way of the React hook useMemo. Out of the box is works great, but if you want to reset your editor back to an empty state (in scenarios where the component is persistent on the page), the undo/redo history will still be available.

Because I wanted to completely reset the editor as if it was the user’s first time interacting with it, I had to figure out how to clear out the history.

While I wasn’t able to find a built in command for it, I did figure out that you can simply reset the history object right on the editor. Said object houses two arrays, one for undos and one for redos.

To reset the history of your editor, simple run:

editor.history = {
  redos: [],
  undos: [],
};

Now the editor won’t have any baggage the next time it’s used!

Join the Conversation

Good stuff? Want more?

Weekly emails about technology, development, and sometimes sauerkraut.

100% Fresh, Grade A Content, Never Spam.

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.

Currently Reading

Parasie Eve

Previous Reads

Buy Me a Coffee Become a Sponsor

Related Articles