Nuxt doesn't update document when data is changed on created
I've this code:
So, by default
showSideBar
is true
. But if the window width is <= 800, then the showSideBar
becomes false.
Now that the value of showSideBar is false the value of showSideBar attribute in main element should change to false.
But it doesn't change27 Replies
You can also run
to confirm that the value changes but the document doesn't update.
@Rifat please attach a stackblitz or codesandbox for these scenarios
it is way easier to provide help
Works fine for me
@manniL / TheAlexLichter nope doesn't work
keep this <= 800 px
Uhm?! 😄
I'm talking about the attribute in main
If you want to act on rezise, you need a listener to the resize event 🙂
no no. Not resize
@manniL / TheAlexLichter check this out https://stackblitz.com/edit/nuxt-discord-rifat-sidebar-1wltgc?file=app.vue
Ahh, sorry
got it
Yeah, hydration error when you do it like that
move it to
beforeMount
The content inside main changed but attribute not changed
you should see such an error when using created + "showing"/rendering the sidebar state
because it expects "same results" from client and server
that's why moving to
onBeforeMount
is the best you can do here
Updated the sandbox
Does that work as expected?No. The attribute doesn't change.
indeed. and still errors
then moving it to
mounted
might be the only way it won't trigger hydration errors
It's slow
I tried it that way. mounted works fine
How do you mean "it is slow"?
It changes after mounting.
Exactly - that's what the name of the lifecycle hook says too 🙂
The problem is: When using SSR, the client expects that the HTML is "the same". If you change values before that, you will see hydration errors
But because you can't infer the width on the server, you can only apply the HTML changes on the client
Is there any way to change it before mounting??
Hmm 🤔
That specifically will be difficult. Not sure.
But you can e.g. set your sidebar to
<ClientOnly>
, then you might not have a weird flash when hiding and could animate it properly when it appears.Thanks. I will try that.
Nuxt doesn't show hydration errors if attribute mismatches. That's why I wasn't able to understand where the problem was. I think it's a bug.
Thanks a lot for helping me with this problem 😊.
Yes, weirdly not.