I'm working on a Vue 3 (Composition API) component where I want users to edit fields by clicking on a text element, and the corresponding input should automatically receive focus.
Here's the expected behavior:
A <div> displays the initial text.
When the user clicks it, the <div> disappears and an <input> appears.
The input must be focused at the moment it becomes visible.
Tested Solutions
Using nextTick() in the editInput function
import { ref, nextTick } from 'vue' const editing = ref(false) const input = ref(null) function editInput() { editing.value = true nextTick(() => { input.value?.focus() }) }Using a v-focus directive with the updated hook
export const vFocus = { updated(el, binding) { if (binding.value) { el.focus() } }}
Along with <input v-show="editing" v-focus="editing" />
Both solutions work real good, now, should I favor nextTick() or the "updated" hook ?
What are the pros/cons of each approach (performance, maintainability)?
Is there an even more “Vue-idiomatic” solution for this scenario?
Thanks