-
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 useRouter() hook and update Router.onChange #370
Conversation
…oks [value, setValue] form
I reversed the tuple values order in the Any thoughts on this @developit ? |
Sorry to bother you again @developit, but would you mind take a look at this one ? |
Up @developit 🌱 👁️ |
Update: Adding a parameter to useRouter() to allow typing the url parameters concerning the route or set of routes your component is in. const [{ matches }] = useRouter<{id: string}>()
matches.id // matches is typed as { id: string } cc @developit |
@prateekbh Do you have any idea of somebody I could ping for this to be reviewed ? |
Hiya! Sorry for the super long wait time on this - I'd read the PR and was noodling on what to do. Two things on my mind:
For the second one, it might actually be a really nice way to get rid of the global import { h, Component } from 'preact';
import { useContext } from 'preact/hooks';
import { Link as StaticLink, exec } from 'preact-router';
/** imagine your useRouter implementation here */
// this was previously 24 lines of code! yay
export function Match(props) {
const { url, path } = useRouter()[0];
const matches = exec(path, props.path, {}) !== false;
return props.children({ url, path, matches });
}
// Link becomes simpler too! and is no longer global
export function Link({ class: c, className, activeClass, activeClassName, path, ...props }) {
const inactive = [c, className].filter(Boolean).join(' ');
const active = [c, className, activeClass, activeClassName].filter(Boolean).join(' ');
const path = useRouter()[0].path;
const matches = exec(path, props.path, {}) !== false;
return <StaticLink {...props} class={matches ? active : inactive} />;
}
export default Match; |
Ah I also thought of one more thing while writing the above - it might be super useful to allow passing a path to function Foo() {
const [{ path, url, matches }] = useRoute('/profile/:username');
if (matches) {
return <Profile username={matches.username} />;
}
return <Homepage />;
} (or maybe this would just be a second hook we could provide?) |
Hi @developit! Thanks for the review and I'm glad that this convinced you to merge it :) Concerning the useRouter hook being exported from
I might be missing something there, don't hesitate to share your thoughts. Concerning the possibility to pass a path to Or we could have another |
Up @developit 😁 |
🎰 😄 @developit |
@developit 👋 😄 |
Would be great to have this merged @developit, if this is okay and because this has been a very long time this PR is opened, I think discussions about enhancing this feature can take place elsewhere after this is merged. Would you agree ? |
@developit I don't know what to do apart from notifying you again, so I notify you again. |
@prateekbh Do you see any way to have this merged ? |
This is frustrating. |
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 fantastic! 🙌
Wow the hook has landed! Fantastic work here @toniopelo 🎉 |
Thanks a lot @marvinhagemeister and @prateekbh, feels good to have this merged finally 😛 💪 |
Hey guys, I see the PR is merged but there isn't any new release/tag version. So are we going to have a new release version to use this new |
@marvinhagemeister Any plan on releasing this soon on npm ? |
@marvinhagemeister @developit Up, could this be released ? |
@prateekbh @developit @marvinhagemeister @masteroftheentireworldandtheuniverse Please, do something, I'm getting really tired with this situation. I would like to be able to |
@toniopelo I think this is published already https://www.runpkg.com/?preact-router@4.0.1/dist/preact-router.module.js |
@JoviDeCroock Wooow that's great. I figured that, without any response to my previous comments, it wasn't yet published. But I was wrong, thanks a lot for the good news 🎉 😄 . |
Fixes #339
This PR adds the
useRouter()
hook that leverage preactuseContext()
.Usage:
This PR also adds
path
andmatches
to theonChange
Router callback.