UNPKG

react-router-ga

Version:

[![Downloads](https://img.shields.io/npm/dm/react-router-ga)](https://npm.im/react-router-ga) [![Version](https://img.shields.io/npm/v/react-router-ga)](https://npm.im/react-router-ga) [![License](https://img.shields.io/npm/l/react-router-ga)](https://ope

55 lines (44 loc) 1.18 kB
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 useHistory() { if (__DEV__) { invariant( typeof useContext === "function", "You must use React >= 16.8 in order to use useHistory()" ); } return useContext(Context).history; } 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 useParams() { if (__DEV__) { invariant( typeof useContext === "function", "You must use React >= 16.8 in order to use useParams()" ); } const match = useContext(Context).match; return match ? match.params : {}; } export function useRouteMatch(path) { if (__DEV__) { invariant( typeof useContext === "function", "You must use React >= 16.8 in order to use useRouteMatch()" ); } return path ? matchPath(useLocation().pathname, path) : useContext(Context).match; }