From 2cf1dd02e39b16acedac05054ed0eef17cb80f95 Mon Sep 17 00:00:00 2001 From: Daniel Skelton Date: Tue, 24 Sep 2019 15:57:33 +0100 Subject: [PATCH 1/2] add useRouter hook --- packages/react-router-dom/.size-snapshot.json | 12 ++--- packages/react-router/.size-snapshot.json | 18 ++++---- .../modules/__tests__/useRouter-test.js | 45 +++++++++++++++++++ packages/react-router/modules/hooks.js | 11 +++++ packages/react-router/modules/index.js | 10 ++++- 5 files changed, 79 insertions(+), 17 deletions(-) create mode 100644 packages/react-router/modules/__tests__/useRouter-test.js diff --git a/packages/react-router-dom/.size-snapshot.json b/packages/react-router-dom/.size-snapshot.json index 69f3b3689f..4af2dc6b4b 100644 --- a/packages/react-router-dom/.size-snapshot.json +++ b/packages/react-router-dom/.size-snapshot.json @@ -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 } } diff --git a/packages/react-router/.size-snapshot.json b/packages/react-router/.size-snapshot.json index 3a066796a8..706ee7a3fa 100644 --- a/packages/react-router/.size-snapshot.json +++ b/packages/react-router/.size-snapshot.json @@ -1,8 +1,8 @@ { "esm/react-router.js": { - "bundled": 24890, - "minified": 14513, - "gzipped": 3839, + "bundled": 25272, + "minified": 14825, + "gzipped": 3868, "treeshaked": { "rollup": { "code": 2389, @@ -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 } } diff --git a/packages/react-router/modules/__tests__/useRouter-test.js b/packages/react-router/modules/__tests__/useRouter-test.js new file mode 100644 index 0000000000..d3a66ae3a9 --- /dev/null +++ b/packages/react-router/modules/__tests__/useRouter-test.js @@ -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("useLocation", () => { + 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( + + + + + , + 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 + }); + }); +}); diff --git a/packages/react-router/modules/hooks.js b/packages/react-router/modules/hooks.js index 364c2029da..55a31019f2 100644 --- a/packages/react-router/modules/hooks.js +++ b/packages/react-router/modules/hooks.js @@ -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); +} diff --git a/packages/react-router/modules/index.js b/packages/react-router/modules/index.js index 5d72f63cdf..bd722f061f 100644 --- a/packages/react-router/modules/index.js +++ b/packages/react-router/modules/index.js @@ -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"; From 6762221370e1280103b81e92a8afc739f036814c Mon Sep 17 00:00:00 2001 From: Daniel Skelton Date: Tue, 24 Sep 2019 15:59:56 +0100 Subject: [PATCH 2/2] update useRouter test description --- packages/react-router/modules/__tests__/useRouter-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-router/modules/__tests__/useRouter-test.js b/packages/react-router/modules/__tests__/useRouter-test.js index d3a66ae3a9..7e697b59c5 100644 --- a/packages/react-router/modules/__tests__/useRouter-test.js +++ b/packages/react-router/modules/__tests__/useRouter-test.js @@ -4,7 +4,7 @@ import { MemoryRouter, Route, useRouter } from "react-router"; import renderStrict from "./utils/renderStrict.js"; -describe("useLocation", () => { +describe("useRouter", () => { const node = document.createElement("div"); afterEach(() => {