astoilkov / use-local-storage-state

React hook that persists data in localStorage
MIT License
1.17k stars 41 forks source link

:sparkles: opt out of cross-tab sync #41

Closed martinsantibanez closed 2 years ago

martinsantibanez commented 2 years ago

As suggested on #24, this enables an option for opting out of multi-tab or multi-window synchronization. I'm not 100% sure about the types, so any help there would be appreciated.

astoilkov commented 2 years ago

Thanks for your contribution @martinsantibanez!

I should admit I'm still wondering if it's a good idea to have an option for that. Can you share your use case? I want to have a list of uses cases before creating this option.

martinsantibanez commented 2 years ago

Thanks to you for maintaining the library! I have a search bar state which is cached. Users can do different searches in different tabs, but only store the one that was most recently updated. I find it a bit similar to what is described in #24 about saving a specific sorting but comparing in multiple tabs. I could as well add a layer on top of the local storage cache which lives only within the tab, but I do think it'd be nice to have in the library.

astoilkov commented 2 years ago

I agree. As this pull request now contains conflicts and I will implement the feature, document it, and include it in the next release.

martinsantibanez commented 2 years ago

Thanks a lot @astoilkov . I look forward for it, and even more for 762fe4f413d5bd0805eab7e8e22bf1b0875d7218 to be released (just had to do a workaround for that one).

Thanks for your work!

martinsantibanez commented 2 years ago

Hi @astoilkov Did you look into this one? or is there any suggested workaround? Thanks in advance!

astoilkov commented 2 years ago

Hi. I'm working on React 18 support and multi-storage support but also have this on my list. You can look at this — https://github.com/astoilkov/use-local-storage-state/issues/46#issuecomment-1079898333. I've realized that all cases are some kind of preference or a setting that you want to apply on initial load so I will be updating the docs and showing how users can do that. I don't see the need to opt-out of cross-tab sync when you can do it easily outside of the repo. Correct me, if I'm wrong. Is there something that can't be easily done outside?

astoilkov commented 2 years ago

Hi @astoilkov Did you look into this one? or is there any suggested workaround? Thanks in advance!

I will implement the option. I experimented a little with different scenarios and you are right. An option will make it a lot easier.

Sorry, for my double change of opinion. I'm a minimalist and I try to keep everything that's not needed out and that sometimes creates situations like this.

astoilkov commented 2 years ago

I've implemented the property — https://github.com/astoilkov/use-local-storage-state/commit/a0869fe772735fcb3df992b2fa743b373b6d1b5b. I will make a release soon.

astoilkov commented 2 years ago

This is now available in the latest 17.2.0 release. Documentation is available at https://github.com/astoilkov/use-local-storage-state/tree/react-17.