Skip to content

Commit

Permalink
Rename useMatch => useRouteMatch
Browse files Browse the repository at this point in the history
Add ability to pass custom path and match exactly like <Route> currently
does.
  • Loading branch information
mjackson committed Sep 20, 2019
1 parent a5dd83f commit 9c784be
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
4 changes: 2 additions & 2 deletions 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";

Expand Down Expand Up @@ -68,7 +68,7 @@ describe("useParams", () => {
}

function Users() {
const match = useMatch();
const match = useRouteMatch();
return (
<div>
<h1>Users</h1>
Expand Down
@@ -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(() => {
Expand All @@ -15,15 +15,13 @@ describe("useMatch", () => {
let match;

function HomePage() {
match = useMatch();
match = useRouteMatch("/home");
return null;
}

renderStrict(
<MemoryRouter initialEntries={["/home"]}>
<Route path="/home">
<HomePage />
</Route>
<HomePage />
</MemoryRouter>,
node
);
Expand Down
27 changes: 15 additions & 12 deletions packages/react-router/modules/hooks.js
Expand Up @@ -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;
}
4 changes: 2 additions & 2 deletions packages/react-router/modules/index.js
Expand Up @@ -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";

0 comments on commit 9c784be

Please sign in to comment.