-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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 for React 16.7+ #6453
Conversation
Still experimental, but no reason we shouldn't be able to have a useRouter hook that works exactly like a path-less <Route>. See #6430
@mjackson Can't we have this released under some |
Co-Authored-By: mjackson <[email protected]>
or name it |
That's a great idea, @Janpot. Let's do that. |
@mjackson Well, there are some other valid comments above, so I am just wondering what's the holdup. If you are busy, I am sure someone can finish this, but I think it's important to let us know. |
@@ -0,0 +1,34 @@ | |||
import React from "react"; | |||
import ReactDOM from "react-dom"; | |||
import { MemoryRouter, Route, useRouter } from "react-router"; |
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.
To fix tests failing :)
import { MemoryRouter, Route, useRouter } from "react-router"; | |
import { MemoryRouter, Route, unstable_useRouter as useRouter } from "react-router"; |
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.
@mjackson Is it really that hard to change this and release it? :) Looks like you are deliberately waiting for React 16.7 to come out and stalling :)
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.
Please be patient, @FredyC. Thanks :)
let context = useContext(RouterContext); | ||
let location = options.location || context.location; | ||
let match = options.path | ||
? matchPath(location.pathname, options) |
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.
You could useMemo
here like:
let match = options.path
? useMemo(() => matchPath(location.pathname, options), [
location.pathname,
options.path,
options.exact,
options.strict
])
: context.match;
So it won't loop through all routes at matchPath
on every usage that route remains unchanged after a render.
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 have a feeling that at the moment the useMemo
came to the light, people are trying to memoize everything :) Bear in mind, that even such memoization has some overhead and without proper benchmarks, it's just a speculation what would be more performant.
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.
Oh and also a conditional use of hook is a big NO-NO, so I really wouldn't do this.
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.
Ops! I'm really getting used to it.
And you're right, it looks like premature optimization for now.
Any update on the status of the PR? |
Would be great to get this merged, as hooks gonna be released soon. |
Hooks are stable now. https://github.com/facebook/react/releases/tag/v16.8.0 |
I think we should just make a new fresh PR. This one needs updating anyway and @mjackson seems busy discovering hooks world 😄 |
Seems like v5, built entirely with hooks, is already under consideration :) |
@mjackson could we get this one released? This would be really useful to avoid render props for matching active state only. Thanks! |
how do we use this from git?
with yarn,
which kiiiiinda makes sense, cause this is a monorepo. |
@NullVoxPopuli I would kinda advise to copy & paste the code from this PR and use that instead. It's fairly simple functionality in a single file and later when it comes out officially, it should be easy to refactor. I would only wish @mjackson would be more communicative regarding this 😕 |
Before considering a full-on rewrite, I would just like to say that the useRouter - hook has great value in and of itself. |
@@ -0,0 +1,20 @@ | |||
import React, { useContext } from "react"; |
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.
Maybe you should remove the unused default React
symbol import ?
Any update on this @mjackson ? Would be great to use hooks for new projects instead of HOC. Thx a lot for this great library by the way! |
We are planning to introduce some hooks soon, but I think
and possibly a few others. We are planning on pushing some of these soon in 5.x |
will |
Still experimental, but no reason we shouldn't be able to have a useRouter hook that works exactly like a path-less
<Route>
.Please don't merge until React 16.7 is out of alpha!Actually, let's do what @Janpot suggested and release this in 4.x asunstable_useRouter
.See #6430