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

Expand options with example for saving a Date object with Typescript example #36

Open
shadoath opened this issue Jul 13, 2023 · 1 comment

Comments

@shadoath
Copy link

Is your feature request related to a problem? Please describe.
I'm working on using this package for the first time and can't figure out the parser options for a Date object in Typescript

Describe the solution you'd like
Expand the Readme or docs to give options for storing more distinct types

Describe alternatives you've considered
I tried:

  const [birthdate, setBirthdate] = useLocalStorage<Date>(
    'birthdate',
    new Date('2005-04-29')
  )

Which returned a string. That was to be expected.

Then I added:

const birthdateOptions = {
  serializer: (obj: any): string => {
    /* Serialize logic */
    console.log(obj, typeof obj)
    return obj.toISOString().split('T')[0]
  },
  parser: (str: any): Date => {
    /* Parse logic */
    console.log(str, typeof str)
    return new Date(str)
  },
}

Which had to use any cause I could not take Date or string or it would cause a mismatch with the hook.

Additional context
I'm looking to understand this package better and make use of it correctly. So asking for some help here and some improved docs to help the next person.

@cfpinto
Copy link

cfpinto commented May 15, 2024

hey. I think parser signature is (val: string) => T | undefined and the serializer signature is (value?: T) => string, if you don't want to use generics replace T with Date and it should work. give it a go

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

No branches or pull requests

2 participants