-
Notifications
You must be signed in to change notification settings - Fork 10
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
feat(atom-with-hash): allow optional setHash
#4
feat(atom-with-hash): allow optional setHash
#4
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 864ff8a:
|
Hi, thanks so much for making suggestions for this library!
I think we can be creative to have new ideas. setHash?: 'default' | 'replaceState' | ((params: URLSearchParams) => void)
We still could have Would be nice to know how you and others think. Let me pin some users who has contributed to |
I think I've figured out what might be going wrong with the Next router when setting the hash in the default setHash. When
|
Nothing much to add but just that this checks out. Looks like NextJS's router logic subscribes to their own I like dai-shi's signature and his idea of keeping |
Sounds good. |
Sure will do. I will make the edits to this PR by end of week. |
## Problem Changing the hash using window.location can be in conflict with the next router. To reproduce this issue within a next app: 1. Use `atomWithHash` somewhere in the app using `{ replaceState: true }` 2. Navigate to the page with `atomWithHash` (using next router) 3. Trigger a change that will invoke setting the hash via `atomWithHash` 3. Navigate in browser going back in history 4. Try and navigate in browser going forward 5. Will behave unexpectedly changing the URL, but not changing the page This might also be unexpected behavior from the perspective of the next router. I'm not entirely sure. But I can imagine other routers possibly also having problems with this. ## Solution Changing the `setHash` function fixed this issue so my proposed solution using these commit's changes are: ```ts import Router from 'next/router'; import { atomWithHash } from 'jotai-location'; atomWithHash('key', 'default', { subscribe: (callback) => { Router.events.on('hashChangeComplete', callback); return () => { Router.events.off('hashChangeComplete', callback); }; }, setHash: (searchParams) => { Router.replace({ pathname: Router.pathname, query: Router.query, hash: searchParams, }); }, }); ``` ## What I don't like about these changes I'm not entirely happy with this API as using `replaceState` with `setHash` makes `replaceState` unused. But for now I wanted to make minimal changes.
As [proposed](jotaijs#4 (comment)) by @dai-shi the `replaceState` option will get a deprecation warning in favor of using the `setHash` option which will default to pushing to history with an option to replace history or a custom setHash function. ```ts setHash?: 'default' | 'replaceState' | ((searchParams: string) => void) ```
420b18c
to
28f3f6f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks very good!
Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>
Published: https://www.npmjs.com/package/jotai-location/v/0.3.0 Thanks for your contribution! Please feel free to work on any other improvements in coding, making examples, and documenting, if you are interested. |
Problem
Changing the hash using window.location can be in conflict with the next router. To reproduce this issue within a next app:
atomWithHash
somewhere in the app using{ replaceState: true }
atomWithHash
(using next router)atomWithHash
This might also be unexpected behavior from the perspective of the next router. I'm not entirely sure. But I can imagine other routers possibly also having problems with this.
Solution
Changing the
setHash
function fixed this issue so my proposed solution using this commit's changes are:What I don't like about these changes
I'm not entirely happy with this API as using
replaceState
withsetHash
makesreplaceState
unused. But for now I wanted to make minimal changes.what do you think?