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

useState is non reactive when ssr: false #27536

Closed
mitjans opened this issue Jun 11, 2024 · 3 comments · Fixed by #27542
Closed

useState is non reactive when ssr: false #27536

mitjans opened this issue Jun 11, 2024 · 3 comments · Fixed by #27542

Comments

@mitjans
Copy link

mitjans commented Jun 11, 2024

Environment


  • Operating System: Darwin
  • Node Version: v20.14.0
  • Nuxt Version: 3.12.1
  • CLI Version: 3.12.0
  • Nitro Version: 2.9.6
  • Package Manager: bun@1.1.12
  • Builder: -
  • User Config: ssr, devtools
  • Runtime Modules: -
  • Build Modules: -

Reproduction

https://stackblitz.com/~/github.com/mitjans/nuxt-repro

Describe the bug

  1. Once the repro app builds, you'll see that the increment button works correctly.
  2. Head to nuxt.config.ts and change ssr: true to ssr: false
  3. Refresh the app
  4. Increment button stops working

Additional context

Further looking into this....

@konstantin-karlovich-unbiased-co-uk

I have the same issue

@mitjans
Copy link
Author

mitjans commented Jun 11, 2024

Looks like the problem is introduced in c545c1d#diff-07c29c3dc0b35942d77d28aed5d8aea7be262649ba1f076712ad8391acf50c79L255-L260

Changing the payload from shallowReactive back to reactive seems to solve the problem.

@danielroe danielroe self-assigned this Jun 11, 2024
@danielroe danielroe changed the title Nuxt 3.12.1 breaks useState in non-ssr apps useState is non reactive when ssr: false Jun 11, 2024
@danielroe
Copy link
Member

danielroe commented Jun 11, 2024

My apologies! Until the next patch, you can work around by creating ~/plugins/payload-reactivity.client.ts with the following content:`

export default defineNuxtPlugin(nuxtApp => {
  const payload = nuxtApp.payload

  for (const key in ['_errors', 'data']) {
    if (!isReactive(payload[key])) {
      payload[key] = shallowReactive(payload[key])
    }
  }

  if (!isReactive(payload.state)) {
    payload.state = reactive(payload.state)
  }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants