astoilkov / use-local-storage-state

React hook that persists data in localStorage
MIT License
1.09k stars 39 forks source link

expose storage object #28

Closed Fxlr8 closed 3 years ago

Fxlr8 commented 3 years ago

Hi, it would be very handy in my case to be able to consistently read from the local storage outside of React scope. Could you please export the storage object?

astoilkov commented 3 years ago

Do you mean the memory storage object? Can you use it by importing it via import storage from 'useLocalStorageState/src/storage'?

Fxlr8 commented 3 years ago

I tried

image
astoilkov commented 3 years ago

Here is a CodeSandbox example showing how you can make it work: https://codesandbox.io/s/todos-example-forked-di7u8?file=/src/App.tsx

Fxlr8 commented 3 years ago

Thank you for the example, but I am still not sure what am I doing wrong here. I am not using create-react-app. Does it have something to do with webpack config or tsconfig?

astoilkov commented 3 years ago

Aha. I think I know the reason. You are probably not on the latest version. Can you try to upgrade and try again?

astoilkov commented 3 years ago

@Fxlr8 I hope you found the reason it wasn't working for you. Let me know if you haven't.

I am closing this issue because it's possible to get access to the storage object without implementing a new API.