Skip to content
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 #6925

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/react-router-dom/.size-snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
}
},
"umd/react-router-dom.js": {
"bundled": 131334,
"minified": 46991,
"gzipped": 14275
"bundled": 131656,
"minified": 47150,
"gzipped": 14295
},
"umd/react-router-dom.min.js": {
"bundled": 87085,
"minified": 29827,
"gzipped": 9885
"bundled": 87264,
"minified": 29901,
"gzipped": 9899
}
}
18 changes: 9 additions & 9 deletions packages/react-router/.size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"esm/react-router.js": {
"bundled": 24890,
"minified": 14513,
"gzipped": 3839,
"bundled": 25272,
"minified": 14825,
"gzipped": 3868,
"treeshaked": {
"rollup": {
"code": 2389,
Expand All @@ -14,13 +14,13 @@
}
},
"umd/react-router.js": {
"bundled": 103056,
"minified": 36690,
"gzipped": 11666
"bundled": 103377,
"minified": 36847,
"gzipped": 11687
},
"umd/react-router.min.js": {
"bundled": 62526,
"minified": 22088,
"gzipped": 7748
"bundled": 62704,
"minified": 22160,
"gzipped": 7762
}
}
45 changes: 45 additions & 0 deletions packages/react-router/modules/__tests__/useRouter-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React from "react";
import ReactDOM from "react-dom";
import { MemoryRouter, Route, useRouter } from "react-router";

import renderStrict from "./utils/renderStrict.js";

describe("useRouter", () => {
const node = document.createElement("div");

afterEach(() => {
ReactDOM.unmountComponentAtNode(node);
});

it("returns the current location, match and history objects", () => {
let routeComponentProps = {};

function HomePage() {
routeComponentProps = useRouter();
return null;
}

renderStrict(
<MemoryRouter initialEntries={["/home"]}>
<Route path="/home">
<HomePage />
</Route>
</MemoryRouter>,
node
);

expect(typeof routeComponentProps.location).toBe("object");
expect(routeComponentProps.location).toMatchObject({
pathname: "/home"
});
expect(typeof routeComponentProps.history).toBe("object");
expect(typeof routeComponentProps.history.push).toBe("function");

expect(typeof routeComponentProps.match).toBe("object");
expect(routeComponentProps.match).toMatchObject({
path: "/home",
url: "/home",
isExact: true
});
});
});
11 changes: 11 additions & 0 deletions packages/react-router/modules/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,14 @@ export function useRouteMatch(path) {
? matchPath(useLocation().pathname, path)
: useContext(Context).match;
}

export function useRouter() {
if (__DEV__) {
invariant(
typeof useContext === "function",
"You must use React >= 16.8 in order to use useRouter()"
);
}

return useContext(Context);
}
10 changes: 8 additions & 2 deletions packages/react-router/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ export { default as generatePath } from "./generatePath";
export { default as matchPath } from "./matchPath";
export { default as withRouter } from "./withRouter";

import { useHistory, useLocation, useParams, useRouteMatch } from "./hooks.js";
export { useHistory, useLocation, useParams, useRouteMatch };
import {
useHistory,
useLocation,
useParams,
useRouteMatch,
useRouter
} from "./hooks.js";
export { useHistory, useLocation, useParams, useRouteMatch, useRouter };

export { default as __RouterContext } from "./RouterContext";