astoilkov / use-local-storage-state

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

React 18 support (created by library author) #47

Closed astoilkov closed 2 years ago

astoilkov commented 2 years ago

I will use this issue to track the progress of implementing React 18 support for this hook. For now, I know of one bug that exists when the new concurrent renderer is enabled.

I will post any progress here.

astoilkov commented 2 years ago

After some research, here are some candidate tasks:

ha-ku commented 2 years ago

After some research, here are some candidate tasks:

  • [ ] implementation should be based on useSyncExternalStore
  • [ ] remove manual handling of hydration mismatches. useSyncExternalStore should handle all that now.
  • [ ] remove manual handling of activeHooks and force updating all other hooks with the same key. useSyncExternalStore will also handle this.
  • [ ] update tests
  • [ ] add tests that test support for concurrent rendering
  • [ ] decide what to do with older versions. it seems like support for both <= v17 and v18 with the same code will have some drawbacks. I should see what will be the most appropriate way to support both versions.

For the last one, React team seems to provide a backwards compatible shim for useSyncExternalStore (see this npm package). Is this suitable here?

astoilkov commented 2 years ago

@ha-ku Yes, it is. However, I'm concerned the shim will double the bundle size of the hook which isn't a no-go but it is something worth considering.

ha-ku commented 2 years ago

@ha-ku Yes, it is. However, I'm concerned the shim will double the bundle size of the hook which isn't a no-go but it is something worth considering.

Indeed. Maybe the way to find the answer is simply making a test version with the shim. Then we'll find out how much the bundle size get influenced. Of cource, this should be something after the implementation with useSyncExternalStore getting finished.

astoilkov commented 2 years ago

Yep, I totally agree. I will do some experiments and probably publish a beta version on npm.

astoilkov commented 2 years ago

This is now complete. The new version supports React 18. Closing this.

martinsantibanez commented 2 years ago

Hi @astoilkov will you keep providing support for React 17?

Thanks again

astoilkov commented 2 years ago

@martinsantibanez Yes. I will be helping people if they have problems. I will make fixes. I am also willing to implement new small features — nothing big.