-
Notifications
You must be signed in to change notification settings - Fork 156
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
Add previous/current route instances to RouterOnChangeArgs #389
base: main
Are you sure you want to change the base?
Add previous/current route instances to RouterOnChangeArgs #389
Conversation
Replaces previous API which just passes a URL for "previous", but a full instance for "current"
This comment has been minimized.
This comment has been minimized.
@@ -26,8 +26,10 @@ export interface RouterOnChangeArgs { | |||
router: Router; | |||
url: string; | |||
previous?: string; | |||
previousRoute?: preact.VNode, |
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.
Can you elaborate on how you'd use this? Just curious.
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.
This is already what current
is. This PR just brings current/previous in parity with one-another. Currently previous
is a string, while current
is a Route instance. Not only is this inconsistent, it makes it hard / impossible to implement logic that uses props from the previous route.
I am depending on these changes in a project of mine, where I use this data to determine whether the user is navigating to/from the default route: use-case.
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.
Ignoring the oddness of calling a browser refresh, why watch the props rather than the route?
Watching component props like this to me sounds like an anti-pattern. I'm not saying that this shouldn't get merged or anything but that there's almost certainly a better way to do what you want.
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.
I'd love to find a work around. I need to know information about the previous route, but I cannot resolve this from the URL. I could manually build up a dictionary of this data myself, keyed by URL, and try and do this lookup myself (including mapping URLs to dictionary keys i.e. trimming query params, etc.), but really it is the responsibility of the Router to give me this information (especially, in my use-case above, whether a Route is the default route or not).
Co-authored-by: Ryan Christian <[email protected]>
Thanks for taking the time to review @rschristian, I thought this would never be looked at |
The nature of this change is to address an issue with RouterOnChangeArgs: the
previous
value is just a URL string, whilecurrent
is a component instance. Not only is it a bit uncomfortable that these are different types (while representing the same concept), it's less useful thatprevious
is just a string, when the previous route instance can be easily stored in the same manner.As far as I know this is a simple, low impact change. I've added another state property (alongside
previousUrl
) calledpreviousRoute
- this is the instance of the previous route i.e. the last value ofcurrent
. As far as I can tell,previousUrl
should map topreviousRoute
1:1 and so storing them both in the same manner should be correct behaviour.In order to not break backwards compatibility with existing applications, I have not replaced
previous
with this change, but rather, added 2 new properties (which would ideally be referenced instead, and maybe one dayprevious
andcurrent
can be removed?) calledpreviousRoute
andcurrentRoute
. You'll noticecurrentRoute
is exactly the same ascurrent
- this is to offer a migration path in the future, and to help the avoid the API feeling "clunky" (a-laDate.getFullYear()
...).Like I said I've deliberately kept this change as low-impact as possible. It could be done in better but more impactful ways, I can make these changes if you wish, just let me know.
Lastly, for some further context, I need this change in my app in order to detect when the user moves to/from a default route (i.e. I am comparing
props.default
in bothpreviousRoute
andcurrentRoute
) - it's not possible to detect this without this change. This would also allow developers to do things like extract prop values from the route as you are leaving it.