question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

onMounted hook running before DOM is available

See original GitHub issue

Environment

  • Operating System: Darwin
  • Node Version: v17.3.1
  • Nuxt Version: 3.0.0-27447229.20f3171
  • Package Manager: yarn@1.22.17
  • Bundler: Vite
  • User Config: vite, build, ssr
  • Runtime Modules: -
  • Build Modules: -

Reproduction

Not needed. The Bug is very clear

Describe the bug

The screenshot below says it all. On first load (refresh) everything is fine and I’m able to run querySelector and access the DOM just fine. However if I navigate to a different route then come back I get null.

<template>
  <div class="TheAdvantages">
    <ul>
      <li class="first" @click="$router.push('')"> I'm first </li>
    </ul>
  </div>
</template>

<script setup lang="ts">
onMounted(() => {
  console.log('onMonuted Hook:')

  console.log(
    "document.querySelector('.first'): ",
    document.querySelector('.first')
  )

  setTimeout(() => {
    console.log('setTimeout 0ms')
    console.log(
      "document.querySelector('.first'): ",
      document.querySelector('.first')
    )
  }, 0)

})
image

Additional context

No response

Logs

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:24
  • Comments:32 (6 by maintainers)

github_iconTop GitHub Comments

7reactions
fanckushcommented, Aug 16, 2022

Workaround - Disable Transitions

in every page where you care about onMounted running accurately add this inside setup:

definePageMeta({
  pageTransition: false,
}) 

notes:

  • I haven’t found a way to globally disable transitions
  • I’m not sure on what i’m missing out by disabling the transition
7reactions
danielrjamescommented, Apr 30, 2022

I had a similar issue where I could not call .focus() on a template ref in onMounted(). I found that the setTimeout hack does work, but it’s pretty messy, so I’m not really a fan of that.

However, I came across https://github.com/nuxt/framework/issues/2801 and found that if you set <NuxtPage :key="$route.fullPath" /> in you app.vue file, it works as you would expect, so no need to setTimeout, etc.

I have no idea why this would be the solution. @danielroe thoughts?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Composition API: Lifecycle Hooks - Vue.js
Registers a callback to be called after the component has been mounted. ... This hook can be used to access the DOM state...
Read more >
Understanding Vue.js Lifecycle Hooks - DigitalOcean
The created hook runs before the templates and Virtual DOM have been mounted or rendered - you are able to access reactive data...
Read more >
vue.js - mounted method is fired before data loaded - VueJS
After Mounted Hook (which means we can access DOM), there is also beforeUpdate and updated hooks. These hooks can be used when data...
Read more >
A Guide to Understanding Vue Lifecycle Hooks - Fjolt
This hook runs immediately before rendering occurs. ... $el is now available, so you can now access and manipulate the DOM from Vue....
Read more >
Lifecycle / onMount • Svelte Tutorial
There are a handful of functions that allow you to run code at key moments ... is onMount , which runs after the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found