justinmahar / react-storage-complete

🗄️ React hooks for accessing localStorage and sessionStorage, with syncing and prefix support. The complete package.
https://justinmahar.github.io/react-storage-complete/
MIT License
8 stars 0 forks source link

Unable to run when using useLocalStorage (Vite) #1

Open RoyRao2333 opened 1 year ago

RoyRao2333 commented 1 year ago

Just got this error and my project crashed:

image

My code:

const userTokenStorageKey = 'USER_TOKEN_STORAGE_KEY';
// Remove this and the project can run properly.
const [, setUserToken] = useLocalStorage<string>(userTokenStorageKey);

const onFinish = useCallback(
    async (values) => {
      // ......
      if (success) {
        if (data.token) {
          setUserToken(data.token);
        }
      }
    },
    [setUserToken],
  );
justinmahar commented 1 year ago

Is this still an issue? I've been using this library in multiple projects with no issues like this. The code itself for the library is just a hook that's being exported, nothing fancy. Was this error during development, or after your app was built, bundled and deployed? Are you using any particular framework for your app?

RoyRao2333 commented 1 year ago

@justinmahar

Thanks for the reply!

So my project is basically written with Vite + React, and nothing particular I guess. This issue is happening during dev, which literally prevents me from running my project.

justinmahar commented 1 year ago

Not sure if it's related, but I just ran into SSR issues using localStorage in production. Just shipped a new version of the library to fix that and SSR issues are history.

To get the latest:

npm i react-storage-complete@latest
RoyRao2333 commented 1 year ago

@justinmahar

Thanks for the effort, but it's still no luck with the latest version... 😢

image

Don't know if this issue is related with Vite or React Router?

justinmahar commented 8 months ago

Closing for now

joe-noumena commented 7 months ago

I am getting the same thing on a Vite project react-storage-complete.js?v=55b96d91:420 Uncaught TypeError: events_1.default is not a constructor

at node_modules/react-storage-complete/dist/hooks/useBrowserStorage.js (react-storage-complete.js?v=55b96d91:420:35)
at __require2 (chunk-AUZ3RYOM.js?v=55b96d91:18:50)
at node_modules/react-storage-complete/dist/index.js (react-storage-complete.js?v=55b96d91:502:18)
at __require2 (chunk-AUZ3RYOM.js?v=55b96d91:18:50)
at react-storage-complete.js?v=55b96d91:507:16

Works fine on a webpack project.

RoyRao2333 commented 7 months ago

@joe-noumena

Thanks for the clarification! So it seems a Vite issue then? @justinmahar

JoeGaffney commented 7 months ago

@RoyRao2333 yeah it seems to be maybe esm issue.

I am trying https://www.npmjs.com/package/use-local-storage-state on the vite project which seems to work. Although not as many features as this package

justinmahar commented 7 months ago

Do either of you have any idea what the heck might be going on with this? I'd love to fix this. I'm bundling 10+ other libraries for npm the exact same way as this one

justinmahar commented 7 months ago

Leaving this here for later, but I think updating the tsconfig for this project might be a start

use-storage-complete tsconfig (this project, working with webpack, issues with Vite)

use-local-storage-state tsconfig (works with Vite)

notes

I'm using cjs, maybe I need to bump that up to esm for this project and all others

cod3rboy commented 4 months ago

Facing the same issue on vite+react+ts project.

justinmahar commented 1 day ago

I ran into some React hydration issues in a Gatsby project (stoicdevotional.com) using this library with the latest React+Gatsby, and I believe I found and fixed the issue as a result. Storage is now initialized after an effect completes. This solved the issues in my project, might do the same for yours. Let us know if you do end up trying it out and it works for you.

To get the latest version:

npm i react-storage-complete@latest