Skip to content

Instantly share code, notes, and snippets.

@xmsi
Last active December 21, 2024 05:43
Show Gist options
  • Save xmsi/bbd3eae3de192a86760eaf578c80964f to your computer and use it in GitHub Desktop.
Save xmsi/bbd3eae3de192a86760eaf578c80964f to your computer and use it in GitHub Desktop.
Vue cheatsheet

nextTick

Author: Gemini

Vue.js uses a reactivity system to efficiently update the DOM when your component's data changes. However, these DOM updates aren't immediate; they happen asynchronously to optimize performance. This means there's a tiny delay between when you change data and when the browser reflects that change.

Why nextTick?

nextTick lets you run code after Vue has finished updating the DOM in response to data changes. This is crucial when you need to:

  • Access or modify DOM elements that depend on updated data: If you try to interact with an element immediately after changing a value that affects it, you might be working with the old version of the DOM. nextTick ensures you're working with the updated DOM.
  • Perform actions that rely on the updated DOM state: Calculations, animations, or interactions that depend on the latest DOM layout often need nextTick.

Example: Focusing an Input Field

Imagine you have a form where a new input field appears after a button click. You want to automatically focus on this new input:

<template>
  <div>
    <button @click="addInput()">Add Input</button>
    <input v-if="showInput" ref="myInput" type="text">
  </div>
</template>

<script>
export default {
  data() {
    return {
      showInput: false,
    };
  },
  methods: {
    addInput() {
      this.showInput = true;
      // Incorrect: Trying to focus before the input is rendered
      // this.$refs.myInput.focus(); 

      this.$nextTick(() => {
        // Correct: Focus after the DOM updates
        this.$refs.myInput.focus(); 
      });
    }
  }
};
</script>

Explanation:

  1. Without nextTick: The this.$refs.myInput.focus() line would execute before Vue has actually added the input to the DOM. this.$refs.myInput would likely be undefined, causing an error.
  2. With nextTick: The focus() call is wrapped in this.$nextTick. This ensures the code runs after Vue has updated the DOM, guaranteeing that this.$refs.myInput refers to the actual input element.

Real-World Scenario: Dynamic Table Heights

In a project with a table that dynamically adds rows, I needed to recalculate the table's height after each row addition to adjust its container. Directly calculating the height after adding a row gave the old height because the DOM hadn't updated yet. nextTick solved this by ensuring the height calculation happened after the DOM update, resulting in the correct height.

Key Takeaway

nextTick is your tool for synchronizing your JavaScript with Vue's DOM updates, preventing timing-related bugs and ensuring your code works with the latest DOM state.

rootState

in vuex you can use rootState to get state of global state in modular state file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment