Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow useLocalStorage mutation callbacks to be called in render phase #584

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jraoult
Copy link

@jraoult jraoult commented Apr 7, 2024

The callbacks returned by useLocalStorage are currently "event callbacks" and, therefore, can't be called in the render phase. It is in contradiction with the recommended approach to adjust state (cf. https://react.dev/learn/you-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes) where it should be possible to do:

function App() {
  const [lsCount, setLsCount] = useLocalStorage('lsCount', 0);

  if (lsCount === 0) {
    // this should work but fails right now
    setLsCount(1);
  }
[...]

This PR removes this limitation.

Copy link

changeset-bot bot commented Apr 7, 2024

⚠️ No Changeset found

Latest commit: 56d9d12

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@jraoult
Copy link
Author

jraoult commented Apr 7, 2024

@juliencrn as promised here's the PR for #549

@jraoult
Copy link
Author

jraoult commented Apr 12, 2024

@juliencrn ping just in case you missed it 😉

@jraoult
Copy link
Author

jraoult commented May 1, 2024

@juliencrn, have you had the time to look at this PR? I'd love to get the ball rolling.

@ddanielcruzz
Copy link

First of all thanks for this awesome project @juliencrn

I was wondering if there any updates regarding merging this?

@kelkes
Copy link

kelkes commented Dec 17, 2024

I would love to have this merge... :)

@jraoult
Copy link
Author

jraoult commented Dec 17, 2024

@kelkes I might be wrong, but I think the commit history of this project doesn't paint a bright future for this library: https://github.com/juliencrn/usehooks-ts/commits/master/, so I would not hold my breath. Avoid it if you have just started using it, or migrate to another solution when possible. If you are only interested in this particular hook, https://usehooks.com/uselocalstorage might be better.

@kelkes
Copy link

kelkes commented Dec 17, 2024

@jraoult i guess you are right... thanks for the alternative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants