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

Initial state #25

Open
danielkcz opened this issue Oct 5, 2019 · 3 comments
Open

Initial state #25

danielkcz opened this issue Oct 5, 2019 · 3 comments

Comments

@danielkcz
Copy link

danielkcz commented Oct 5, 2019

I definitely like the idea and mostly of how it's implemented, but the somewhat big caveat to me as DRY guy is repeating the initial state. I mean with regular useState it does make sense as it's isolated but looking at this like a "shared" state, there should definitely be a single place to declare initialState.

I would propose to expand API like createPersistentState(key, initialState). I know there is another argument for storage, but that's likely to be used rarely, so it can be at 3rd place just fine :) Alternatively, it could make sense to export another function with currying behavior where storage would be specified in the first argument and it would return factory bound to that storage type.

Of course, it would still be possible to specify an initial state directly in a component, but it would be optional. Perhaps it could even show DEV-only warning to keep such state unified.

@karlsander
Copy link

I'm very interested in this behavior. Do you want this as PR @donavon or is it more of a fork addition?

@FMGordillo
Copy link

FMGordillo commented Feb 28, 2020

+1

I have new Set() as initialState foruseState(), and in every moment that I persist to localStorage I have to make transformations (not very performant). Adding a transform param also might come in handy idk

@astoilkov
Copy link

astoilkov commented Mar 14, 2020

I have made a module that solves this problem by making a special function called createLocalStorageStateHook(key, initialState?) you can take a look at it https://github.com/astoilkov/use-local-storage-state.

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

4 participants