Hydration Mismatch when changing state #1867
-
Reproductionhttps://stackblitz.com/edit/nuxt-starter-ayzr1f?file=pages/[...slug].vue Steps to reproduce the bugI have a header store. There I have a state to know, if my header component should be display in black or in white. I then have different stage modules, where I set the header state to true or false, to display the right css styles.
Expected behaviorI would have expected, that the state is directly false and not first true and then false. I think it has something to do with ssr hydration, but I didn't find anything in the docs, if I made anything wrong. Actual behaviorI would have expected, that the state ist directly false, because I set it in directly in the setup function of the stage module to false. Additional informationThanks a lot for looking into it. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
This is due to the order of execution of your components: all except Stage.vue are rendering before you change the To solve this, you need to ensure the In general, avoid setting a global settings that are hydrated in arbitrary components' |
Beta Was this translation helpful? Give feedback.
-
@posva Thanks so much for the fast answer. So I could solve this by puting the setting of isLight to false in an onMounted function to ensure, that every component is ready. This would solve the hydration mismatch, but of course it still would be true for a short time and then false, when mounted. So as you wrote the only solution would be, to check in the app.vue, if my page contains a specific Stage and then set the state to false. Of course this means, I have to find those component and can't add the state change to the component itself. As I understand you right, there is no option to do this on a component level. |
Beta Was this translation helpful? Give feedback.
This is due to the order of execution of your components: all except Stage.vue are rendering before you change the
isLight
to true. So they render with the initial value offalse
and, on server, never render again. That's whyStage.vue
is the only copmonent rendering withfalse
initially, because it's the only one being able to see that change. On client, thefalse
value is picked up on hydration, so all components render withfalse
, causing a Hydration mismatch.To solve this, you need to ensure the
isLight
setting is set before any component relying on it is rendered.In general, avoid setting a global settings that are hydrated in arbitrary components'
setup()
as it will create Hydrati…