vueuse icon indicating copy to clipboard operation
vueuse copied to clipboard

BUG | `useLocalStorage` | pinia中使用useLocalStorage,结果默认值覆盖了本地缓存值

Open f754699 opened this issue 9 months ago • 7 comments

Describe the bug

代码 export const useShouyeStore = defineStore('shouye', () => { // 激活的分组 const activeGroup = useLocalStorage("activeGroup", "", {initOnMounted: true}) return { activeGroup, } }) 我再localstorage里面写入值 打开网页后,本地缓存被默认值覆盖


I write values into local storage. After opening the webpage, the local cache is overwritten by the default values.

Reproduction

System Info

System:                                            
    OS: Windows 10 10.0.19045                        
    CPU: (20) x64 12th Gen Intel(R) Core(TM) i7-12700
    Memory: 2.36 GB / 15.69 GB                       
  Binaries:                                          
    Node: 20.18.3 - C:\Program Files\nodejs\node.EXE 
    Yarn: 1.22.22 - C:\Program Files\nodejs\yarn.CMD 
    npm: 10.8.2 - C:\Program Files\nodejs\npm.CMD    
    pnpm: 9.15.4 - C:\Program Files\nodejs\pnpm.CMD  
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (134.0.3124.51)
    Internet Explorer: 11.0.19041.1566
  npmPackages:
    @vueuse/nuxt: ^13.0.0 => 13.0.0
    vue: ^3.5.13 => 3.5.13

Used Package Manager

pnpm

Validations

  • [x] Follow our Code of Conduct
  • [x] Read the Contributing Guidelines.
  • [x] Read the docs.
  • [x] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • [x] Make sure this is a VueUse issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/core instead.
  • [x] Check that this is a concrete bug. For Q&A open a GitHub Discussion.
  • [x] The provided reproduction is a minimal reproducible example of the bug.

f754699 avatar May 23 '25 01:05 f754699

Can you please create a minimal reproduction?

You can use this Playground as a starting point

OrbisK avatar May 23 '25 07:05 OrbisK

https://codesandbox.io/p/devbox/flamboyant-field-fp4f6n?embed=1

f754699 avatar May 23 '25 08:05 f754699

Works outside of the pinia store for me: https://codesandbox.io/p/devbox/flamboyant-field-fp4f6n?embed=1

OrbisK avatar May 23 '25 08:05 OrbisK

嘛意思

f754699 avatar May 23 '25 10:05 f754699

its basically because pinia is detecting the initialState as {} for whatever reason

maybe you need to set activeGroup: activeGroup.value or something.

it isn't a bug. i need to run but i can try take another look when im back online 👍

43081j avatar May 23 '25 13:05 43081j

嘛意思

Sorry, code sandbox share link seems to be broken, if the sandbox is forked.

https://codesandbox.io/p/devbox/condescending-murdock-wz6hdz?file=%2Fapp.vue

OrbisK avatar May 23 '25 13:05 OrbisK

its basically because pinia is detecting the initialState as {} for whatever reason

maybe you need to set activeGroup: activeGroup.value or something.

it isn't a bug. i need to run but i can try take another look when im back online 👍

I think this is releated to SSR. I think pinia keeps the initial value when rendered serverside

OrbisK avatar May 23 '25 13:05 OrbisK