Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Lifecycle Hooks  ›  Lesson

The Lifecycle and onMounted

Lifecycle Hooks 13 minEvery component is set up, mounted into the DOM, updated and finally unmounted, and onMounted runs once its elements exist.

The Lifecycle and onMounted

When Shelf opens, the cursor should already be blinking in the search box. You write searchInput.value.focus() in your script, and the console says "Cannot read properties of null". The input exists in your template, so why is it missing? Because your code ran before Vue had created it. Timing is what lifecycle hooks are for.

The rule

Setup runs first; the DOM exists only from onMounted on.

Every component instance lives through the same stages:

  1. Setup. <script setup> runs once. Refs, computeds and functions are created. No DOM yet.
  2. Before mount. onBeforeMount runs just before the elements are created and inserted.
  3. Mounted. onMounted runs after the component's elements are in the page. Template refs now point at real elements.
  4. Updates. When reactive state changes, the component re-renders, with onBeforeUpdate and onUpdated around it. This can happen many times.
  5. Unmount. When the component is removed, onBeforeUnmount and then onUnmounted run.

Hooks are functions you import from vue and call with a callback:

<script setup>
import { ref, onMounted } from 'vue'

const searchInput = ref(null)   // filled when the element exists

onMounted(() => {
  searchInput.value.focus()
})
</script>

<template>
  <input ref="searchInput" type="search" placeholder="Find a book" />
</template>

The ref="searchInput" attribute is a template ref: after mounting, Vue puts the real <input> element into the ref with the same name. (Vue 3.5 also offers useTemplateRef('searchInput') for the same job.) Before mounting it is null, which is exactly the error from the introduction.

Typical onMounted work: focusing an element, measuring sizes, starting a chart or map library that needs a real element, and adding listeners to window. Loading data can start earlier, right in setup, since it does not need the DOM.

One rule: register hooks synchronously during setup. Calling onMounted inside a setTimeout, or in any callback that runs later, is too late; Vue no longer knows which component the hook belongs to.

The shortcut

For any piece of setup code, ask "Does it need a real element?" Yes → put it inside onMounted. No → it can run directly in setup.

Sorting Shelf's start-up codeStep 1 of 4
  1. Create the books ref

    Pure data, no element needed. Directly in setup.

Drills

Answer fast — trust the rule.

quick drill

When does onMounted run?

quick drill

In setup, searchInput.value is null. Why?

quick drill

Which hook runs after a re-render caused by a state change?

quick drill

Which of these registers a hook correctly?

Remember

  • The stages are setup, mount, update (repeated) and unmount; onMounted runs once the DOM exists.
  • Shortcut: "does it need a real element?" Yes means onMounted.
  • Trap: touching template refs during setup, or registering hooks asynchronously.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainFocus the search box

When Shelf opens, the search input should be focused. A teammate calls searchInput.value.focus() directly in script setup and gets an error. Explain why and fix it.