Search code examples
next.jslocal-storagezustand

TypeError: Cannot read properties of undefined (reading 'clearStorage') - NextJs


In my NextJs component, I am using Zustand persist for localstorage in browsers.

and I use a Shadcn UI button below, but NextJs shows errors below in my terminal:

components/stateInput.tsx (40:75) @ clearStorage
TypeError: Cannot read properties of undefined (reading 'clearStorage')
<Button className='destructive-color' onClick={useItemsStore.persist.clearStorage}>Clear Storage</Button>

I tried to use

const clearStorage = (): void => {
    useItemsStore.persist.clearStorage();
}
...
<Button className='destructive-color' onClick={clearStorage}>Clear Storage</Button>

Solution

  • Somehow it works

    const clearStorage = (): void => {
        useItemsStore.persist.clearStorage();
    }
    

    after restarting NextJs, wait for a few seconds, then refresh the page... then maybe restart NextJs again...

    Hopefully, NextJs can run faster next time...