2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Nuxt 3.12.1 の useState の不具合

Last updated at Posted at 2024-06-13

こんな簡単なコードでも正常に動かない

<script setup lang="ts">
const counter = useState('counter', () => 0)
</script>

<template>
  <div>
    Counter: {{ counter }}
    <button @click="counter++">+</button>
    <button @click="counter--">-</button>
  </div>
</template>

暫定解決

plugins/payload-reactivity.client.ts ファイルを以下の内容で作成すると動くようになった

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)
  }

})

正式対応

次のバージョンを待つのが良さそう

Issue

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

プロフィール・経歴

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?