Russo
Russo

Reputation: 2992

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>

Upvotes: 0

Views: 256

Answers (1)

Russo
Russo

Reputation: 2992

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...

Upvotes: 0

Related Questions