soyguijarro / react-storage-hooks

React hooks for persistent state
https://npm.im/react-storage-hooks
MIT License
166 stars 18 forks source link

useLocalStorageReducer state by default is undefined #8

Closed AdamSalma closed 4 years ago

AdamSalma commented 5 years ago
   const MyComponent = () => {
     const [state, dispatch, writeError] = useLocalStorageReducer(
        key,
        reducer,
       {
            count: 1
       }
     );

     console.log(state)
     return null
  }

This initially logs state as undefined a few times until dispatch is called, which is causing unexpected behaviour with my code.

Is this intentional?

soyguijarro commented 4 years ago

Hi @AdamSalma, thanks for reporting!

Let me see if I'm understanding this: the key in your storage has no value and you're getting undefined a few times before getting { count: 1}, is that right? That does sound like a bug. I'm working on a full rewrite of the library, so I'll keep this in mind so that it's fixed in the new version.

soyguijarro commented 4 years ago

@AdamSalma This should be fixed in version 4.0.0 that I've just published, so I'm closing this issue. Feel free to reopen if it keeps happening.