Skip to content
Tutorials Center
My learning

Topics

Lesson 85 of 87 ยท Practice and Review

Common Vue Mistakes and How to Fix Them

Seven Vue 3 mistakes almost everyone makes - forgotten .value, lost reactivity, mutated props, bad keys, v-if with v-for and async races - each with a runnable fix.

Vue is forgiving, and that is exactly why its bugs are so confusing: the page renders, nothing crashes, and yet a number refuses to update or a text box shows the wrong word. Almost all of these bugs come from a small set of misunderstandings about how reactivity and the template work together. Once you can name the pattern, each one takes a minute to fix.

This lesson walks through seven of them. Each section shows the broken code, what really happens, why, and the fix, always as an example you can run and change. Examples whose title ends with "(fails on purpose)" are meant to misbehave. The previews use the production build of Vue, which is small and quiet: it skips the helpful warnings that Vue's development build prints (a Vite project gives you that build while you develop). So below, "the console" means the errors that even the production build reports, and where a warning exists only in development, the text says so. Fix warnings in development instead of ignoring them.

Mistake 1: forgetting .value (or adding it where it does not belong)

A ref is a small object with a .value property. In JavaScript code you read and write .value. In a template, top-level refs are unwrapped for you, so you write the bare name. Mixing these two rules up is the number one beginner bug. Our ref lesson explains why refs exist.

Forgetting .value in script, adding it in the template

In script, count + 1 does not add anything: the ref object is converted to text ([object Object]) and glued to 1. In the template, count is already unwrapped to the number 1, so count.value is undefined and Vue renders it as an empty string (the brackets above show the emptiness). The rule: .value in script, no .value in the template. The exception is a ref nested inside a plain object, such as state.count where state = { count: ref(0) }: the template does not unwrap that one, so there you do need .value.

Mistake 2: destructuring a reactive object

Reactivity lives in the proxy, not in the numbers it holds. When you pull a property out with destructuring you get a plain copy of the value at that moment, and the connection is gone.

flowchart LR
  S["state (reactive proxy)"] -->|"read in the template"| T["tracked: re-renders on change"]
  S -->|"const { count } = state"| C["count: a plain number"]
  C -->|"read in the template"| N["not tracked: stays stale"]

Reading through the proxy is tracked; a destructured copy is just a number.

Destructuring loses the connection (fails on purpose)

After the code runs, state.count shows 3 and the toRefs version shows 3, but the destructured count still shows 0, and it stays at 0 however often you press the button. toRefs(state) turns every property into a ref that points back into the proxy, so destructuring those is safe. The same problem hits props destructured inside a plain setup(props) function. Read them as props.title, or wrap a getter in computed. The reactivity pitfalls lesson lists more variations.

Mistake 3: replacing a reactive object

A reactive() proxy is a fixed object. If you throw it away and assign a new one to your variable, the template still points at the old proxy.

Reassigning reactive versus assigning ref.value

Press the button. The ref line changes to Grace; the reactive line stays on Ada, because setup handed the old object to the template and reassigning the local variable did not change that. When you need to replace a whole object (for example with a server response), keep it in a ref and assign .value, or keep the reactive object and copy fields into it with Object.assign(profile, newData). More on choosing in reactive objects.

Mistake 4: mutating a prop

Data flows down from parent to child. A child may read a prop but must not write to it, because the next time the parent re-renders, its value would overwrite the child's change. In the development build, Vue enforces this with a readonly proxy and a warning; the production build does not.

A child that edits its prop (fails on purpose)

Click the button. The badge now reads "Hello!", yet the parent still holds "Hello": the two copies of the value have drifted apart, and the next time the parent re-renders with a new label, the child's edit is thrown away. Vue's development build catches this and prints Set operation on key "label" failed: target is readonly. (the production build used in these previews stays silent, which makes the bug easier to miss). There are two correct designs. If the child only needs a starting value, copy the prop into its own ref. If the parent owns the data, the child asks for a change with an event, and the parent updates its state:

Two correct ways to change a prop value

The first button counts from its own copy of start. The second asks the parent to change text, and the new value flows back down as the prop. This is exactly what v-model on a component does for you, covered in component v-model. See also props.

Mistake 5: bad key values in v-for

key is the identity of a list item. Vue uses it to decide which DOM element belongs to which item when the list changes. Using the index as the key says "the first row is always the same thing", which is false as soon as you remove the first item. Type a word into every box, then press "Remove first".

Index keys versus id keys

Type "hello" next to Apple in both lists and remove the first item. In the index-key list, "hello" is still on screen but now sits next to Banana: Vue reused the first row's input for the new first item. In the id-key list it vanishes with Apple, which is correct. Use a stable, unique value from the data (item.id). An index is safe only for lists that are never reordered, filtered or edited in the middle. A related slip: :key="Math.random()" creates a new identity on every render, so every row is destroyed and rebuilt, losing focus and state each time. More in list rendering.

Mistake 6: v-if on the same element as v-for

In Vue 3, when both directives sit on one element, v-if is evaluated first, so it cannot see the loop variable. A filter like v-if="!task.done" then fails because task does not exist yet.

v-if with v-for, broken and fixed

The broken app renders nothing and the console reports an error (Cannot read properties of undefined (reading 'done'), a TypeError from the render function). The fix is to filter in a computed property and loop over the result. When you need the condition per item and cannot filter, put the v-if on a <template v-for> wrapper's inner element instead. A computed filter is also faster, since it runs once instead of once per row. See conditional rendering.

Mistake 7: async surprises

Vue updates the DOM asynchronously and in batches, and your own async code can finish in any order. Two classic consequences follow.

Reading the DOM too early

Changing state does not change the page immediately. The update is queued and applied once, on the next tick, so many changes in a row cost a single render.

Reading the DOM before and after nextTick

The list gains an item with push, but the DOM still has one li until await nextTick() resolves, then it has two. Use nextTick whenever you measure or focus an element right after changing state.

Responses that arrive out of order

When the user types, you send a request per keystroke. A slow early request can finish after a fast later one and overwrite the newer result with stale data.

sequenceDiagram
  participant U as User
  participant A as App
  participant S as Server
  U->>A: types v
  A->>S: request 1 (slow)
  U->>A: types vu
  A->>S: request 2 (fast)
  S-->>A: response 2 for vu
  S-->>A: response 1 for v overwrites it

The last response to arrive wins, not the last request that was sent.

A search race and its fix

Wait half a second. The naive version ends on "results for v" (the slow, outdated response arrived last), while the guarded one shows "results for vu". The guard is a counter: each call remembers its number and only writes its result if it is still the latest call. In real code you would also cancel the old request with an AbortController, as shown in fetching data.

A debugging checklist

When something in a Vue page does not update, walk through these questions in order:

  1. Is the value reactive at all (ref, reactive, computed), or a plain variable or a destructured copy?
  2. In script, did you use .value? In the template, did you leave it off?
  3. Did you replace a whole reactive object instead of changing its fields?
  4. Is a child writing to a prop instead of emitting an event?
  5. Does every v-for item have a stable, unique key?
  6. Are you reading the DOM before nextTick, or trusting an async result that may be stale?
  7. Is the console open? Development-build warnings usually name the exact problem (see debugging Vue).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Inside setup(), const n = ref(5). Which expression gives the number 6?

    In script you must read .value. n + 1 turns the ref object into text and appends 1. Inside a template, a top-level ref is unwrapped, so {{ n + 1 }} would also show 6, but that is template code, not script.
  2. 2. const { count } = reactive({ count: 0 }) and later the object's count changes. What does the destructured count hold?

    Destructuring copies the value at that moment. Use toRefs(state) (or toRef) to get refs that stay linked, or read state.count directly.
  3. 3. A child component needs to change a value owned by its parent. What is the right approach?

    Props are readonly in the child. The child emits an event (or uses defineModel / v-model), the parent changes its own state, and the new value flows back down as the prop.
  4. 4. Which key is the best choice for v-for rows that can be removed from the middle of the list?

    A stable id lets Vue match each DOM node to its item. Index keys shift when rows are removed, and random keys force every row to be rebuilt on each render.
  5. 5. A v-for element also has v-if="!task.done" and fails with a TypeError. Why?

    In Vue 3 v-if has the higher priority, so it runs without the loop variable. Filter with a computed property and loop over the filtered list.

Summary

  • Use .value in script and the bare name in templates; a ref nested inside a plain object is not unwrapped.
  • Destructuring a reactive object (or props in setup) copies values and breaks reactivity; use toRefs, toRef or read through the object.
  • Do not reassign a reactive variable; use a ref and assign .value, or copy fields in with Object.assign.
  • Props are readonly: copy them into local state or emit events, and let the owner of the data change it.
  • Give every v-for item a stable, unique key from the data, never the index for editable lists and never a random value.
  • Never put v-if and v-for on the same element; filter with computed.
  • DOM updates are batched, so use nextTick before reading the DOM, and guard async results against stale responses.

Next: Vue.js Interview Questions