From 9c784bef16df910597f70763a710ad2f662521ca Mon Sep 17 00:00:00 2001 From: Michael Jackson Date: Thu, 19 Sep 2019 17:42:28 -0700 Subject: [PATCH] Rename useMatch => useRouteMatch Add ability to pass custom path and match exactly like currently does. --- .../modules/__tests__/useParams-test.js | 4 +-- ...useMatch-test.js => useRouteMatch-test.js} | 10 +++---- packages/react-router/modules/hooks.js | 27 ++++++++++--------- packages/react-router/modules/index.js | 4 +-- 4 files changed, 23 insertions(+), 22 deletions(-) rename packages/react-router/modules/__tests__/{useMatch-test.js => useRouteMatch-test.js} (76%) diff --git a/packages/react-router/modules/__tests__/useParams-test.js b/packages/react-router/modules/__tests__/useParams-test.js index c80358a087..5dc2583f15 100644 --- a/packages/react-router/modules/__tests__/useParams-test.js +++ b/packages/react-router/modules/__tests__/useParams-test.js @@ -1,6 +1,6 @@ import React from "react"; import ReactDOM from "react-dom"; -import { MemoryRouter, Route, useMatch, useParams } from "react-router"; +import { MemoryRouter, Route, useParams, useRouteMatch } from "react-router"; import renderStrict from "./utils/renderStrict.js"; @@ -68,7 +68,7 @@ describe("useParams", () => { } function Users() { - const match = useMatch(); + const match = useRouteMatch(); return (

Users

diff --git a/packages/react-router/modules/__tests__/useMatch-test.js b/packages/react-router/modules/__tests__/useRouteMatch-test.js similarity index 76% rename from packages/react-router/modules/__tests__/useMatch-test.js rename to packages/react-router/modules/__tests__/useRouteMatch-test.js index 9e31afd7f5..8ae4616fa0 100644 --- a/packages/react-router/modules/__tests__/useMatch-test.js +++ b/packages/react-router/modules/__tests__/useRouteMatch-test.js @@ -1,10 +1,10 @@ import React from "react"; import ReactDOM from "react-dom"; -import { MemoryRouter, Route, useMatch } from "react-router"; +import { MemoryRouter, useRouteMatch } from "react-router"; import renderStrict from "./utils/renderStrict.js"; -describe("useMatch", () => { +describe("useRouteMatch", () => { const node = document.createElement("div"); afterEach(() => { @@ -15,15 +15,13 @@ describe("useMatch", () => { let match; function HomePage() { - match = useMatch(); + match = useRouteMatch("/home"); return null; } renderStrict( - - - + , node ); diff --git a/packages/react-router/modules/hooks.js b/packages/react-router/modules/hooks.js index 1a4935fd81..364c2029da 100644 --- a/packages/react-router/modules/hooks.js +++ b/packages/react-router/modules/hooks.js @@ -2,49 +2,52 @@ import React from "react"; import invariant from "tiny-invariant"; import Context from "./RouterContext.js"; +import matchPath from "./matchPath.js"; const useContext = React.useContext; -export function useMatch() { +export function useHistory() { if (__DEV__) { invariant( typeof useContext === "function", - "You must use React >= 16.8 in order to use useMatch()" + "You must use React >= 16.8 in order to use useHistory()" ); } - return useContext(Context).match; + return useContext(Context).history; } -export function useParams() { +export function useLocation() { if (__DEV__) { invariant( typeof useContext === "function", - "You must use React >= 16.8 in order to use useParams()" + "You must use React >= 16.8 in order to use useLocation()" ); } - return useMatch().params; + return useContext(Context).location; } -export function useLocation() { +export function useParams() { if (__DEV__) { invariant( typeof useContext === "function", - "You must use React >= 16.8 in order to use useLocation()" + "You must use React >= 16.8 in order to use useParams()" ); } - return useContext(Context).location; + return useContext(Context).match.params; } -export function useHistory() { +export function useRouteMatch(path) { if (__DEV__) { invariant( typeof useContext === "function", - "You must use React >= 16.8 in order to use useHistory()" + "You must use React >= 16.8 in order to use useMatch()" ); } - return useContext(Context).history; + return path + ? matchPath(useLocation().pathname, path) + : useContext(Context).match; } diff --git a/packages/react-router/modules/index.js b/packages/react-router/modules/index.js index 2dcf60e2a3..5d72f63cdf 100644 --- a/packages/react-router/modules/index.js +++ b/packages/react-router/modules/index.js @@ -32,7 +32,7 @@ export { default as generatePath } from "./generatePath"; export { default as matchPath } from "./matchPath"; export { default as withRouter } from "./withRouter"; -import { useMatch, useParams, useLocation, useHistory } from "./hooks.js"; -export { useMatch, useParams, useLocation, useHistory }; +import { useHistory, useLocation, useParams, useRouteMatch } from "./hooks.js"; +export { useHistory, useLocation, useParams, useRouteMatch }; export { default as __RouterContext } from "./RouterContext";