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

Session Storage support? #9

Closed
natac13 opened this issue Jul 30, 2020 · 13 comments
Closed

Session Storage support? #9

natac13 opened this issue Jul 30, 2020 · 13 comments
Labels
discussion Discussions on library API decisions

Comments

@natac13
Copy link

natac13 commented Jul 30, 2020

Is there any plans to let the user choose between localstorage and session storage. Or even between using the default value over the stored value?

I have a use case that I would only like to use the stored value during the same user session.

@astoilkov astoilkov added the discussion Discussions on library API decisions label Jul 30, 2020
@astoilkov
Copy link
Owner

astoilkov commented Jul 30, 2020

Is there any plans to let the user choose between localstorage and session storage.

I am considering supporting session storage. I am not sure about it yet.

Or even between using the default value over the stored value?

Can you clarify on that? I'm not sure I understand it correctly.

I have a use case that I would only like to use the stored value during the same user session.

Is it possible that you share your use case? I am interested in why keeping it in memory doesn't work for you.

@natac13
Copy link
Author

natac13 commented Jul 30, 2020

Is it possible that you share your use case? I am interested in why keeping it in memory doesn't work for you.

Maybe using memory is the answer for me.

Can you clarify on that? I'm not sure I understand it correctly.

The use case is a landing page with full page sections, on scroll I set a page number from useState. The full page section then changes. Upon clicking a nav link and then using the back button I would like the user to be on the same full page section of the landing page. However when they close the tab or navigate directly to the home page via url I would like to show the 0 full page section.

@astoilkov
Copy link
Owner

Yes, it seems you can do this without session storage.

I am closing this issue. If I have misunderstood something please feel free to write back.

@Taraluktus
Copy link

One advantage for sessionStorage over in-memory is, that it survives page reload, and it behaves at tab clone/duplicate as a user would expect it.
localStorage could do that too, but it's a bit too "persistent" for our use case.

@astoilkov
Copy link
Owner

@Taraluktus Are you teammates with @natac13? I am asking because the use case @natac13 described can be done with hash URLs. This way the position of the section will be remembered on page reload and tab duplication.

@Taraluktus
Copy link

Taraluktus commented Jul 31, 2020

No, totally unrelated to him.
We only use localStorage/sessionStorage for the JWT token (not ideal for a URL hash).

Not to mention Safari's latest changes to localStorage handling, and of course private browsing.

@astoilkov
Copy link
Owner

astoilkov commented Jul 31, 2020

@Taraluktus Do you use any kind of library which abstracts the session storage logic?

@Taraluktus
Copy link

No, before useLocalStorageState, we used sessionStorage directly.

@astoilkov
Copy link
Owner

@Taraluktus How did you swap using sessionStorage with using localStorage? Isn't sessionStorage the thing you need?

@Taraluktus
Copy link

Taraluktus commented Aug 2, 2020

They work nearly the same, and also have the same API (but I think you already knew that).
We wanted to use a hook-based solution, and this one is the most mature. Nevermind, we are now taking a custom approach since the use case is very specific.

@astoilkov
Copy link
Owner

@Taraluktus Thank you for the time taken to answer my questions. This has been very useful. I will leave the implementation of sessionStorage for now and wait for more feedback.

@Taraluktus
Copy link

Fair enough.
There is another caveat: the storage DOM event is not fired for sessionStorage, which makes it hard to track changes. A one-to-one implementation (replacing localStorage with sessionStorage) would not work as expected.

@astoilkov
Copy link
Owner

Hi all,

For people interested, I've published a use-session-storage-state package. The repo — https:/astoilkov/use-session-storage-state.

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

No branches or pull requests

3 participants