New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
onMounted hook running before DOM is available #13471
Comments
EDIT: check this comment for a better workaround For anyone else having this issue until it's resolved you can use `setTimeout` Instead of: onMounted(() => {
... code that needs DOM // WON'T WORK on route change
}) Do this: onMounted(() => {
setTimeout(() => {
... code that needs DOM // WORKS!!
}, 1) // 1 seems to work better for me than 0
}) |
I just ran into the same thing, working with the PayPal SDK. Hard refreshes work fine, and soft navigation works fine as well, but the second navigation calls The |
Adding these links for my future self's reference (or anyone else who wants to take this on): This is not reproducible with |
I made a reactive variable that is null and changes to mounted. And the parent container wrapped v-if. |
@the-m-git could you provide a snippet? |
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 #13270 and found that if you set I have no idea why this would be the solution. @danielroe thoughts? |
@danielrjames thanks! interesting workaround I imagine this to affect performance, because vue won't get to reuse anything really... so while this might be more stable than setTimeout it's certainly still just a workaround |
I agree. I do think the advantage of setting the key on |
It seems this is in fact a vue core bug: vuejs/core#5844. Disabling transitions can be a workaround for now. |
@danielroe is there a way to disable transitions globally? |
…o trying a vanilla method to wait for element
Any updates regarding this issue? It seems like this should be unacceptable in such a big production oriented framework.. |
This is not a Nuxt issue but an upstream Vue bug: vuejs/core#5844. It needs to be resolved within Vue itself (and in fact there is a in-progress PR). Please track updates there. |
Thanks for sharing this workaround ;D For some reason, I could only get it working by accessing if (!doSomethingWithDom()) {
// if couldn't do it right away,
// attach the callback to the root element, which onAfterEnter has access to
rootEl.value.$el.onMounted = doSomethingWithDom
} |
I saw people in here mentioning this hook but why not use it? its simple and clean.
|
Will this method be guaranteed to run after |
Guys, the upsteam issue was fixed yesterday. Look forward to the next vue release 👀 |
it is guaranteed, but you will need an initial execute of the code that needs the DOM, because you only get page transitions when navigating inside your app and not when you access the website. So the hook will not activate when you open your Website. :) But the issues is getting a fix anyway so no worries |
They fixed it yesterday but we don't know when they're releasing?? Almost unreal I just stumbled on this problem and the issue is being solved now...ish |
I'm sure you'll be delighted to hear that this should now be resolved in vue 3.3.7. If you still encounter any issues related to this, please open a new issue with a minimal reproduction. 🙏 |
3.3.7 has just been released 2 days ago :) |
@danielroe it seems like the upstream issue got fixed in vue version 3.3.7. Is nuxt latest version already running with that version? So that we should expect it to work? |
@awacode21 yes you already get the newest vue version when you install nuxt now, you can simply update your project and get the newest vue version too ( yes the bugs are fixed, i tried it myself ;) ) |
@yodler1 thanks for the feedback! Because i am trying to use https://bryntum.com/products/grid/ with Nuxt 3 and i run into an issue (bryntum/support#5527) where they are referring to this problem. But i updated to latest nuxt version and the problem still occurs. So i guess their issue is not related. Must be something different then. Thank you. |
I still have this problem too. (cc. @awacode21) You try to use the useFetch function on the onMounted hook, but it always responds null. Both �Nuxt and Vue are up to date. @danielroe Is this a different issue? |
The most common mistake would be that you're not awaiting your |
@zestlee1106 You should not use |
@danielroe Thank you for your response! I used those methods in the And I'm going to wrap the So I want to make sure the wrapped method doesn't matter if it's called from a server or a client. Inside the |
Similar issue, but I use <script setup lang="ts">
const { data: groups, pending, error } = await useFetch('/api/groups')
</script> And whenever I reload the page, the data is My current workaround is to convert it to |
@danielroe Hello! We see that after SSR-generated countent is first loaded, useFetch returns null during onMounted hook. But then in SPA mode (going to different page and return to firstly loaded page) useFetch return data during onMounted hook? What is the difference and why this works so? |
@danielroe Hello again. We've found following solution: Could you, please, help us to understand such a behavior? Here's our solution's example: |
@shumiarrow It's recommended not to use useFetch anywhere except at the root of <script setup>. If you need to fetch data within a function, composable, or a Pinia store, you should use $fetch instead. In your case, there's no need to wrap useFetch in the onMounted hook because useFetch executes when the component is initialized. Using useFetch is pretty much the same as using onMounted with $fetch inside, but with an added benefit: useFetch is reactive and will rerun if the reactive parameters you pass to it change, or if the URL is dynamic. On the other hand, onMounted will not rerun even if the parameters passed to $fetch change. I recommend you watch this excellent video recently made by @manniL that explains this concept, as it's a very common misunderstanding: And here, you can find more explanations in the Nuxt 3 documentation: |
@XStarlink Thanks a lot for your answer and all links you've sent. |
This issue still exists when navigating to different layouts, by the way, |
Dear nuxt team, the issue wasn't fixed fully and still exists. If there is multiple nested pages then issue appears again. It seems like fix only solved issue with the fist level of nesting |
@suleymanyaryev The best thing would be to create a new issue with a reproduction 🙏 |
Workarounds
until this is fixed in vuejs/core#5952) you can use:
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 getnull
.The text was updated successfully, but these errors were encountered: