-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- useMatch - useParams - useLocation - useHistory
- Loading branch information
Showing
8 changed files
with
285 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,26 @@ | ||
{ | ||
"esm/react-router-dom.js": { | ||
"bundled": 8797, | ||
"minified": 5223, | ||
"gzipped": 1682, | ||
"bundled": 9444, | ||
"minified": 5645, | ||
"gzipped": 1786, | ||
"treeshaked": { | ||
"rollup": { | ||
"code": 379, | ||
"import_statements": 355 | ||
"code": 2272, | ||
"import_statements": 432 | ||
}, | ||
"webpack": { | ||
"code": 1612 | ||
"code": 3572 | ||
} | ||
} | ||
}, | ||
"umd/react-router-dom.js": { | ||
"bundled": 129625, | ||
"minified": 46107, | ||
"gzipped": 14073 | ||
"bundled": 131334, | ||
"minified": 46991, | ||
"gzipped": 14275 | ||
}, | ||
"umd/react-router-dom.min.js": { | ||
"bundled": 86293, | ||
"minified": 29465, | ||
"gzipped": 9792 | ||
"bundled": 87085, | ||
"minified": 29827, | ||
"gzipped": 9885 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,26 @@ | ||
{ | ||
"esm/react-router.js": { | ||
"bundled": 23515, | ||
"minified": 13336, | ||
"gzipped": 3696, | ||
"bundled": 24890, | ||
"minified": 14513, | ||
"gzipped": 3839, | ||
"treeshaked": { | ||
"rollup": { | ||
"code": 2376, | ||
"code": 2389, | ||
"import_statements": 470 | ||
}, | ||
"webpack": { | ||
"code": 3743 | ||
"code": 3758 | ||
} | ||
} | ||
}, | ||
"umd/react-router.js": { | ||
"bundled": 102011, | ||
"minified": 36085, | ||
"gzipped": 11534 | ||
"bundled": 103056, | ||
"minified": 36690, | ||
"gzipped": 11666 | ||
}, | ||
"umd/react-router.min.js": { | ||
"bundled": 62277, | ||
"minified": 21931, | ||
"gzipped": 7707 | ||
"bundled": 62526, | ||
"minified": 22088, | ||
"gzipped": 7748 | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
packages/react-router/modules/__tests__/useHistory-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, useHistory } from "react-router"; | ||
|
||
import renderStrict from "./utils/renderStrict.js"; | ||
|
||
describe("useHistory", () => { | ||
const node = document.createElement("div"); | ||
|
||
afterEach(() => { | ||
ReactDOM.unmountComponentAtNode(node); | ||
}); | ||
|
||
it("returns the history object", () => { | ||
let history; | ||
|
||
function HomePage() { | ||
history = useHistory(); | ||
return null; | ||
} | ||
|
||
renderStrict( | ||
<MemoryRouter initialEntries={["/home"]}> | ||
<Route path="/home"> | ||
<HomePage /> | ||
</Route> | ||
</MemoryRouter>, | ||
node | ||
); | ||
|
||
expect(typeof history).toBe("object"); | ||
expect(typeof history.push).toBe("function"); | ||
}); | ||
}); |
36 changes: 36 additions & 0 deletions
36
packages/react-router/modules/__tests__/useLocation-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React from "react"; | ||
import ReactDOM from "react-dom"; | ||
import { MemoryRouter, Route, useLocation } from "react-router"; | ||
|
||
import renderStrict from "./utils/renderStrict.js"; | ||
|
||
describe("useLocation", () => { | ||
const node = document.createElement("div"); | ||
|
||
afterEach(() => { | ||
ReactDOM.unmountComponentAtNode(node); | ||
}); | ||
|
||
it("returns the current location object", () => { | ||
let location; | ||
|
||
function HomePage() { | ||
location = useLocation(); | ||
return null; | ||
} | ||
|
||
renderStrict( | ||
<MemoryRouter initialEntries={["/home"]}> | ||
<Route path="/home"> | ||
<HomePage /> | ||
</Route> | ||
</MemoryRouter>, | ||
node | ||
); | ||
|
||
expect(typeof location).toBe("object"); | ||
expect(location).toMatchObject({ | ||
pathname: "/home" | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import React from "react"; | ||
import ReactDOM from "react-dom"; | ||
import { MemoryRouter, Route, useMatch } from "react-router"; | ||
|
||
import renderStrict from "./utils/renderStrict.js"; | ||
|
||
describe("useMatch", () => { | ||
const node = document.createElement("div"); | ||
|
||
afterEach(() => { | ||
ReactDOM.unmountComponentAtNode(node); | ||
}); | ||
|
||
it("returns the match object", () => { | ||
let match; | ||
|
||
function HomePage() { | ||
match = useMatch(); | ||
return null; | ||
} | ||
|
||
renderStrict( | ||
<MemoryRouter initialEntries={["/home"]}> | ||
<Route path="/home"> | ||
<HomePage /> | ||
</Route> | ||
</MemoryRouter>, | ||
node | ||
); | ||
|
||
expect(typeof match).toBe("object"); | ||
expect(match).toMatchObject({ | ||
path: "/home", | ||
url: "/home", | ||
isExact: true | ||
}); | ||
}); | ||
}); |
101 changes: 101 additions & 0 deletions
101
packages/react-router/modules/__tests__/useParams-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import React from "react"; | ||
import ReactDOM from "react-dom"; | ||
import { MemoryRouter, Route, useMatch, useParams } from "react-router"; | ||
|
||
import renderStrict from "./utils/renderStrict.js"; | ||
|
||
describe("useParams", () => { | ||
const node = document.createElement("div"); | ||
|
||
afterEach(() => { | ||
ReactDOM.unmountComponentAtNode(node); | ||
}); | ||
|
||
describe("when the path has no params", () => { | ||
it("returns an empty hash", () => { | ||
let params; | ||
|
||
function HomePage() { | ||
params = useParams(); | ||
return null; | ||
} | ||
|
||
renderStrict( | ||
<MemoryRouter initialEntries={["/home"]}> | ||
<Route path="/home"> | ||
<HomePage /> | ||
</Route> | ||
</MemoryRouter>, | ||
node | ||
); | ||
|
||
expect(typeof params).toBe("object"); | ||
expect(Object.keys(params)).toHaveLength(0); | ||
}); | ||
}); | ||
|
||
describe("when the path has some params", () => { | ||
it("returns a hash of the URL params and their values", () => { | ||
let params; | ||
|
||
function BlogPost() { | ||
params = useParams(); | ||
return null; | ||
} | ||
|
||
renderStrict( | ||
<MemoryRouter initialEntries={["/blog/cupcakes"]}> | ||
<Route path="/blog/:slug"> | ||
<BlogPost /> | ||
</Route> | ||
</MemoryRouter>, | ||
node | ||
); | ||
|
||
expect(typeof params).toBe("object"); | ||
expect(params).toMatchObject({ | ||
slug: "cupcakes" | ||
}); | ||
}); | ||
|
||
describe("a child route", () => { | ||
it("returns a combined hash of the parent and child params", () => { | ||
let params; | ||
|
||
function Course() { | ||
params = useParams(); | ||
return null; | ||
} | ||
|
||
function Users() { | ||
const match = useMatch(); | ||
return ( | ||
<div> | ||
<h1>Users</h1> | ||
<Route path={`${match.path}/courses/:course`}> | ||
<Course /> | ||
</Route> | ||
</div> | ||
); | ||
} | ||
|
||
renderStrict( | ||
<MemoryRouter | ||
initialEntries={["/users/mjackson/courses/react-router"]} | ||
> | ||
<Route path="/users/:username"> | ||
<Users /> | ||
</Route> | ||
</MemoryRouter>, | ||
node | ||
); | ||
|
||
expect(typeof params).toBe("object"); | ||
expect(params).toMatchObject({ | ||
username: "mjackson", | ||
course: "react-router" | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React from "react"; | ||
import invariant from "tiny-invariant"; | ||
|
||
import Context from "./RouterContext.js"; | ||
|
||
const useContext = React.useContext; | ||
|
||
export function useMatch() { | ||
if (__DEV__) { | ||
invariant( | ||
typeof useContext === "function", | ||
"You must use React >= 16.8 in order to use useMatch()" | ||
); | ||
} | ||
|
||
return useContext(Context).match; | ||
} | ||
|
||
export function useParams() { | ||
if (__DEV__) { | ||
invariant( | ||
typeof useContext === "function", | ||
"You must use React >= 16.8 in order to use useParams()" | ||
); | ||
} | ||
|
||
return useMatch().params; | ||
} | ||
|
||
export function useLocation() { | ||
if (__DEV__) { | ||
invariant( | ||
typeof useContext === "function", | ||
"You must use React >= 16.8 in order to use useLocation()" | ||
); | ||
} | ||
|
||
return useContext(Context).location; | ||
} | ||
|
||
export function useHistory() { | ||
if (__DEV__) { | ||
invariant( | ||
typeof useContext === "function", | ||
"You must use React >= 16.8 in order to use useHistory()" | ||
); | ||
} | ||
|
||
return useContext(Context).history; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
d6224d6
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.
Just discovered by accident that you added hooks to RR. Any plans in which version of RR you will release them?
d6224d6
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.
useRouter would be useful as well
d6224d6
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.
@sibelius Created
useRouter
hook here https://github.com/ReactTraining/react-router/pull/6925/filesd6224d6
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 just installed React router 5.1 yesterday, but cannot find where to import the new hooks. Can You guys help me?
d6224d6
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.
@dattebayorob
If you're using TypeScript be sure to update
@types/react-router
as welld6224d6
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.
Got it, thanks
d6224d6
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.
@sibelius If you have a valid use case, please go ahead and open an issue. We can discuss there π
d6224d6
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.
@visormatt it's
useRouteMatch
and notuseMatch
. Just for correctnessd6224d6
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.
useRouter
is more or less congruent to whatwithRouter
was doing. Please consider this a +1 thatuseRouter
should be included.Not only is it more consistent with what users are already expecting from
withRouter
, but it leads to less re-reading of the react-router context when you need to use multiple items from the context. Separately exposing each item from the context seems sub-optimal (you by definition, end up using multipleuseContext
calls).