Weekly Vue News #119 - Simple Pinia History With Undo and Redo Functionality

Weekly Vue News #119

Simple Pinia History With Undo and Redo Functionality

Hi 👋

I have no personal news for you, still busy working on the editor update for CodeSnap 🙈

Have a lovely week ☀️


To support me:

Vue
📕 Vue Patterns
👉🏻 Free guide for Vue patterns that can significantly help in writing clean, efficient, and maintainable code.
👉🏻 Includes patterns like composables, provide/inject, and more.
www.patterns.dev
📹 Is your function REALLY a Vue composable?
👉🏻 In this video, Alex explains the difference between a plain function and a Vue composable.
www.youtube.com
📹 Vue 3 & Composition API - Full Project
👉🏻 Build an expense tracker application from scratch using Vue.js 3 and the composition API with the latest syntax from version 3.2.
www.youtube.com
📹 Vue.js (Vue v3) for Beginners
👉🏻 A mega three-hour workshop on YouTube.
www.youtube.com
🛠️ Interactive Example in Vue Macros
👉🏻 You can try previewing different syntaxes for Vue, including defining props, emits, render functions, and components.
vue-macros.dev/interactive/

Nuxt
📹 Data Fetching With Nuxt 3
👉🏻 This video covers the two composables ( useFetch() & useAsyncData() ) used to fetch data within Nuxt 3.
www.youtube.com
📕 Nuxt Social Share Module
👉🏻 This module helps you to easily integrate social share buttons to your app.
github.com

📅 Events
Vuejs Amsterdam 2024
👉🏻 28 - 29 February 2024, Amsterdam
vuejs.amsterdam

💬 Quote of the week

🔥 Pinia Tip: Simple History With Undo and Redo Functionality

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:

stores/counter.ts
1import { ref } from 'vue';
2import { defineStore } from 'pinia';
3
4export const useCounterStore = defineStore('counter', () => {
5 const count = ref(0);
6
7 function increment() {
8 count.value++;
9 }
10
11 function decrement() {
12 count.value--;
13 }
14
15 return { count, increment, decrement };
16});

Now let's use that store in a Vue component:

components/Counter.vue
1<script setup lang="ts">
2const counterStore = useCounterStore();
3
4const { count } = storeToRefs(counterStore);
5</script>
6
7<template>
8 <div class="container">
9 <span>count is {{ counterStore.count }}</span>
10 <button @click="counterStore.increment">Increment</button>
11 <button @click="counterStore.decrement">Decrement</button>
12 </div>
13</template>
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:

components/Counter.vue
1<script setup lang="ts">
2const counterStore = useCounterStore();
3
4const { count } = storeToRefs(counterStore);
5
6const { history, undo, redo } = useRefHistory(count, {
7 capacity: 50, // limit history records
8});
9</script>
10
11<template>
12 <div class="container">
13 <span>count is {{ counterStore.count }}</span>
14 <button @click="counterStore.increment">Increment</button>
15 <button @click="counterStore.decrement">Decrement</button>
16 </div>
17
18 <div class="container" style="margin-top: 20px">
19 <span>History</span>
20 <button @click="undo">Undo</button>
21 <button @click="redo">Redo</button>
22
23 <ul>
24 <li v-for="entry of history" :key="entry.timestamp">
25 <span>{{ entry }}</span>
26 </li>
27 </ul>
28 </div>
29</template>

Try it yourself in the following StackBlitz:

😂 Fun

🧑🏻‍💻 In Other News
📕 Tech Interview Handbook
👉🏻 Curated coding interview preparation materials.
github.com
📕 How TechCrunch Spent $1 Million Rebuilding Their Website
👉🏻 Ever wondered how a website project can cost so much?👇🏻
www.pootlepress.com
📕 Bad Licenses
👉🏻 A compendium of poorly written or absurd open-source licenses.
github.com
🛠️ background-removal-js
👉🏻 Remove backgrounds from images directly in the browser or Node.js environment with ease and no additional costs or privacy concerns.
github.com

Comments? Join the discussion about this issue here.

Until next week,

Unsubscribe from this newsletter
Holzapfelkreuther Str. 19, 81375 Munich, Germany

评论

此博客中的热门博文

Scripting News: Tuesday, June 11, 2024

Scripting News: Tuesday, February 13, 2024