Skip to content

Commit

Permalink
fix: re-exported relevant history types
Browse files Browse the repository at this point in the history
- Also upgraded history dependency
- Changed `location.state` types to `any` to match the new history types
  • Loading branch information
ryanflorence committed Nov 1, 2021
1 parent b7559b2 commit 767b306
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
18 changes: 10 additions & 8 deletions packages/react-router-dom/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from "react";
import type { BrowserHistory, HashHistory, State, To } from "history";
import type { BrowserHistory, HashHistory } from "history";
import { createBrowserHistory, createHashHistory, createPath } from "history";
import {
MemoryRouter,
Expand All @@ -25,6 +25,7 @@ import {
useResolvedPath,
useRoutes
} from "react-router";
import type { To } from "react-router";

function warning(cond: boolean, message: string): void {
if (!cond) {
Expand Down Expand Up @@ -74,6 +75,10 @@ export {
};

export type {
Location,
Path,
To,
NavigationType,
MemoryRouterProps,
NavigateFunction,
NavigateOptions,
Expand Down Expand Up @@ -196,7 +201,7 @@ function isModifiedEvent(event: React.MouseEvent) {
export interface LinkProps
extends Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, "href"> {
replace?: boolean;
state?: State;
state?: any;
to: To;
}

Expand Down Expand Up @@ -322,10 +327,7 @@ if (__DEV__) {
* you need to create custom `<Link>` compoments with the same click behavior we
* use in our exported `<Link>`.
*/
export function useLinkClickHandler<
E extends Element = HTMLAnchorElement,
S extends State = State
>(
export function useLinkClickHandler<E extends Element = HTMLAnchorElement>(
to: To,
{
target,
Expand All @@ -334,7 +336,7 @@ export function useLinkClickHandler<
}: {
target?: React.HTMLAttributeAnchorTarget;
replace?: boolean;
state?: S;
state?: any;
} = {}
): (event: React.MouseEvent<E, MouseEvent>) => void {
let navigate = useNavigate();
Expand Down Expand Up @@ -400,7 +402,7 @@ export function useSearchParams(defaultInit?: URLSearchParamsInit) {
let setSearchParams = React.useCallback(
(
nextInit: URLSearchParamsInit,
navigateOptions?: { replace?: boolean; state?: State }
navigateOptions?: { replace?: boolean; state?: any }
) => {
navigate("?" + createSearchParams(nextInit), navigateOptions);
},
Expand Down
23 changes: 15 additions & 8 deletions packages/react-router-native/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as React from "react";
import type { State, To } from "history";
import {
BackHandler,
GestureResponderEvent,
Expand Down Expand Up @@ -27,11 +26,14 @@ import {
useLocation,
useMatch,
useNavigate,
useNavigationType,
useOutlet,
useParams,
useResolvedPath,
useRoutes
} from "react-router";
import type { To } from "react-router";

import URLSearchParams from "@ungap/url-search-params";

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -57,29 +59,34 @@ export {
useLocation,
useMatch,
useNavigate,
useNavigationType,
useOutlet,
useParams,
useResolvedPath,
useRoutes
};

export type {
IndexRouteProps,
LayoutRouteProps,
Location,
MemoryRouterProps,
NavigateFunction,
NavigateOptions,
NavigateProps,
NavigationType,
Navigator,
OutletProps,
Params,
Path,
PathMatch,
PathRouteProps,
RouteMatch,
RouteObject,
RouteProps,
PathRouteProps,
LayoutRouteProps,
IndexRouteProps,
RouterProps,
RoutesProps
RoutesProps,
To
} from "react-router";

///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -119,7 +126,7 @@ export interface LinkProps extends TouchableHighlightProps {
children?: React.ReactNode;
onPress?: (event: GestureResponderEvent) => void;
replace?: boolean;
state?: State;
state?: any;
to: To;
}

Expand Down Expand Up @@ -156,14 +163,14 @@ const URLEventType = "url";
* you need to create custom `<Link>` compoments with the same press behavior we
* use in our exported `<Link>`.
*/
export function useLinkPressHandler<S extends State = State>(
export function useLinkPressHandler(
to: To,
{
replace,
state
}: {
replace?: boolean;
state?: S;
state?: any;
} = {}
): (event: GestureResponderEvent) => void {
let navigate = useNavigate();
Expand Down
9 changes: 5 additions & 4 deletions packages/react-router/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type {
Location,
MemoryHistory,
Path,
State,
To
} from "history";
import {
Expand All @@ -14,6 +13,8 @@ import {
parsePath
} from "history";

export type { Location, Path, To, NavigationType };

function invariant(cond: any, message: string): asserts cond {
if (!cond) throw new Error(message);
}
Expand Down Expand Up @@ -147,7 +148,7 @@ export function MemoryRouter({
export interface NavigateProps {
to: To;
replace?: boolean;
state?: State;
state?: any;
}

/**
Expand Down Expand Up @@ -443,7 +444,7 @@ export interface NavigateFunction {

export interface NavigateOptions {
replace?: boolean;
state?: State;
state?: any;
}

/**
Expand Down Expand Up @@ -474,7 +475,7 @@ export function useNavigate(): NavigateFunction {
});

let navigate: NavigateFunction = React.useCallback(
(to: To | number, options: { replace?: boolean; state?: State } = {}) => {
(to: To | number, options: { replace?: boolean; state?: any } = {}) => {
warning(
activeRef.current,
`You should call navigate() in a React.useEffect(), not when ` +
Expand Down
2 changes: 1 addition & 1 deletion packages/react-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"react": ">=16.8"
},
"dependencies": {
"history": "^5.0.1"
"history": "^5.0.3"
},
"sideEffects": false,
"keywords": [
Expand Down

0 comments on commit 767b306

Please sign in to comment.