I recently needed to implement a simple history with undo and redo functionality for a project I'm working on. I decided to use the useRefHistory composable from VueUse to implement this functionality.
Defining the store
Let's first take a look at the store we use for demonstration purposes which is a simple counter store:
Now let's use that store in a Vue component:
Implementing the history
Now that we have a store, let's implement the history functionality. We can do this by using the useRefHistory
composable from VueUse:
Demo
Try it yourself in the following StackBlitz:
If you liked this Vue tip, follow me on X to get notified about new tips, blog posts, and more. Alternatively (or additionally), you can subscribe to my weekly Vue & Nuxt newsletter:
Vue Tip: Accessing Template Ref in Child Component
Learn how to access template refs in a child component within a Vue.js application, enabling you to manipulate and interact with specific elements in the child component's template from the parent component.
Vue Tip: Automatically Import Components
Use the unplugin-vue-components tool to automatically import your Vue components.