Skip to content
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

Solve Hydration node mismatch for useStorage #3447

Closed
4 tasks done
iBobik opened this issue Oct 2, 2023 · 1 comment · Fixed by #3504
Closed
4 tasks done

Solve Hydration node mismatch for useStorage #3447

iBobik opened this issue Oct 2, 2023 · 1 comment · Fixed by #3504
Labels
enhancement New feature or request

Comments

@iBobik
Copy link

iBobik commented Oct 2, 2023

Clear and concise description of the problem

When using useStorage for app state like this:

export const useOrder = () => useLocalStorage<Order>('order', { items: [] })

The SSR will render UI with default value and client will render UI with saved state which will render to Hydration node mismatch warning.

Suggested solution

It would be nice if I can solve it like this:

export const useOrder = () => useLocalStorage<Order>('order', { items: [] }, { ssrUseDefault: true })

Which will set default value first and wait for page:finish hook.

Alternative

Workaround:

export function useOrder () {
  const order = ref(defaultOrder)
  useNuxtApp().hook('page:finish', () => {
    const storedOrder = useLocalStorage<Order>('order', defaultOrder)
    order.value = storedOrder.value
  })
  return order
}

Additional context

Maybe print warning when ssrUseDefault option was used in non-Nuxt environment.

Validations

@iBobik iBobik added the enhancement New feature or request label Oct 2, 2023
posva added a commit to posva/vueuse that referenced this issue Oct 25, 2023
@posva
Copy link
Contributor

posva commented Oct 25, 2023

A possible implementation would be to setup the listeners within onMounted() so they are only applied once the app has rendered. I opened #3504

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants