- to reuse function in many places
DOCS: https://vuejs.org/guide/reusability/composables.html#side-effects
composable.js
import { ref, onMounted, onUnmounted } from 'vue'
// by convention, composable function names start with "use"
export function useMouse() {
// state encapsulated and managed by the composable
const x = ref(0)
e.t.c
// expose managed state as return value
return { x, y }
}and in client
<script setup>
import { useMouse } from './mouse.js'
const { x, y } = useMouse()
</script>
<template>Mouse position is at: {{ x }}, {{ y }}</template>https://share.google/aimode/rQWpyO6eXe9KGWJAg
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.
nextTickensures 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:
- Without
nextTick: Thethis.$refs.myInput.focus()line would execute before Vue has actually added the input to the DOM.this.$refs.myInputwould likely beundefined, causing an error. - With
nextTick: Thefocus()call is wrapped inthis.$nextTick. This ensures the code runs after Vue has updated the DOM, guaranteeing thatthis.$refs.myInputrefers 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.
in vuex you can use rootState to get state of global state in modular state file