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

Update useSessionStorage to support strings + add new options + add useReadSessionStorage #340

Closed
wants to merge 2 commits into from

Conversation

jonlinkens
Copy link

@jonlinkens jonlinkens commented Jun 26, 2023

Fixes #335

Attempts to make useSessionStorage less opinionated in how session storage values are stored and parsed.

Changes

  • Add third options argument to useSessionStorage:

    • parseAsJson: boolean - default to true, pass as false when you don't want a value to be parsed using JSON.parse. This will fix the bug I was describing where a value has been previously set as a string
    • parser: (value: string | null) => T - a custom parser if you have stored a session storage value with a custom serializer function
    • serializer: (value: T) => string - a custom serializer to store values in a format different from the default behavior of JSON.stringify
  • Renamed initialValue to defaultValue for readability

  • Removed undefined value support - I can't imagine a case where this is needed over just not storing the key/value in session storage + removing this decreases complexity of the hook

  • Add useReadSessionStorage, similar to useReadLocalStorage

Add new options object argument with parseAsJson, parser, serializer props
@changeset-bot
Copy link

changeset-bot bot commented Jun 26, 2023

⚠️ No Changeset found

Latest commit: 4cf78c4

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

@vercel
Copy link

vercel bot commented Jun 26, 2023

Someone is attempting to deploy a commit to a Personal Account owned by @juliencrn on Vercel.

@juliencrn first needs to authorize it.

@jonlinkens jonlinkens changed the title Update useSessionStorage to support strings + add new options, Update useSessionStorage to support strings + add new options + add useReadSessionStorage Jun 26, 2023
@jonlinkens
Copy link
Author

jonlinkens commented Dec 18, 2023

Going to close this as it seems this library is no longer maintained Apologies, will reopen! 😊

@jonlinkens jonlinkens closed this Dec 18, 2023
@jonlinkens jonlinkens reopened this Jan 16, 2024
@juliencrn juliencrn added bug Something isn't working V3 Something introduces breaking changes. labels Jan 22, 2024
@jonlinkens
Copy link
Author

jonlinkens commented Jan 26, 2024

I think this can now be closed as addressed in #439? I'll leave open so you can confirm - Thank you very much btw!

@juliencrn
Copy link
Owner

juliencrn commented Jan 31, 2024

I think this can now be closed as addressed in #439? I'll leave open so you can confirm - Thank you very much btw!

Yes indeed! Your PR was good, I, unfortunately, worked on it before seeing your one.
I will add you to the contributor list, Thank you 👍

@juliencrn juliencrn closed this Jan 31, 2024
@jonlinkens
Copy link
Author

I, unfortunately, worked on it before seeing your one

No worries at all, just happy to see the bug fixed - thanks for your hard work on this library

@erichartline
Copy link

Any chance that we could still bring in the useReadSessionStorage hook from this PR? We have the equivalent for localStorage but not sessionStorage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working V3 Something introduces breaking changes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

useSessionStorage returns undefined if the value is already set as a string (+ some suggestions)
3 participants