imbhargav5 / rooks

Essential React custom hooks ⚓ to super charge your components!
https://rooks.vercel.app
MIT License
3.19k stars 216 forks source link

useLocalstorageState: Ensure the identity of set/remove don't change when used #1714

Closed nathggns closed 1 year ago

nathggns commented 1 year ago

In https://github.com/imbhargav5/rooks/commit/adaaad1725977d539f5bda8c1fc8ee8358bd6a49#diff-b0add5bafe9b472c2eb73a766b1fde5062317a10e944f82725c1d782ccc7d6bf the possibility of using a function to generate the next state for useLocalstorageState was added. Unfortunately, this was done by adding the current value as a dependency of set, which meant its identity changed every time it is used. This is not the behaviour of useState and is not ideal – and most importantly its not necessary. We can just read the value from a ref instead.

This PR does that, and adds extra tests to ensure the identity of set (and remove) doesn't change when used.

codesandbox[bot] commented 1 year ago

CodeSandbox logoCodeSandbox logo  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

vercel[bot] commented 1 year ago

@nathggns is attempting to deploy a commit to the Rooks Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] commented 1 year ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
rooks ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 18, 2023 8:07am
imbhargav5 commented 1 year ago

Thanks @nathggns this is super useful! I appreciate it 🤩