-
-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React from "react"; | ||
import ReactDOM from "react-dom"; | ||
import { MemoryRouter, Route, useRouter } from "react-router"; | ||
|
||
import renderStrict from "./utils/renderStrict"; | ||
|
||
describe("useRouter", () => { | ||
let node = document.createElement("div"); | ||
|
||
afterEach(() => { | ||
ReactDOM.unmountComponentAtNode(node); | ||
}); | ||
|
||
it("provides the <Route> props", () => { | ||
let router; | ||
function TestComponent() { | ||
router = useRouter(); | ||
return null; | ||
} | ||
|
||
renderStrict( | ||
<MemoryRouter initialEntries={["/one"]}> | ||
<Route path="/:id" component={TestComponent} /> | ||
</MemoryRouter>, | ||
node | ||
); | ||
|
||
expect(router).toBeDefined(); | ||
expect(router.match.url).toBe("/one"); | ||
expect(router.match.path).toBe("/:id"); | ||
expect(router.match.params).toBeDefined(); | ||
expect(router.match.params.id).toBe("one"); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import React, { useContext } from "react"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe you should remove the unused default |
||
import invariant from "tiny-invariant"; | ||
|
||
import RouterContext from "./RouterContext"; | ||
import matchPath from "./matchPath"; | ||
|
||
export default function useRouter(options = {}) { | ||
invariant( | ||
mjackson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
typeof useContext === "function", | ||
"The useRouter hook requires React 16.7 or greater" | ||
); | ||
|
||
let context = useContext(RouterContext); | ||
let location = options.location || context.location; | ||
pshrmn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let match = options.path | ||
? matchPath(location.pathname, options) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have a feeling that at the moment the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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. |
||
: context.match; | ||
|
||
return { ...context, location, match }; | ||
mjackson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 :)
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 :)