Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split navigator from location context #7936

Merged
merged 1 commit into from
Aug 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions packages/react-router/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const readOnly: <T extends unknown>(obj: T) => T = __DEV__
? obj => Object.freeze(obj)
: obj => obj;

function invariant(cond: boolean, message: string): void {
function invariant(cond: any, message: string): asserts cond {
chaance marked this conversation as resolved.
Show resolved Hide resolved
if (!cond) throw new Error(message);
}

Expand Down Expand Up @@ -66,14 +66,15 @@ export type Navigator = Omit<
"action" | "location" | "back" | "forward" | "listen"
>;

const NavigatorContext = React.createContext<Navigator>(null!);

const LocationContext = React.createContext<LocationContextObject>({
static: false
});

interface LocationContextObject {
action?: Action;
location?: Location;
navigator?: Navigator;
static: boolean;
}

Expand Down Expand Up @@ -240,10 +241,12 @@ export function Router({
);

return (
<LocationContext.Provider
children={children}
value={{ action, location, navigator, static: staticProp }}
/>
<NavigatorContext.Provider value={navigator}>
<LocationContext.Provider
children={children}
value={{ action, location, static: staticProp }}
chaance marked this conversation as resolved.
Show resolved Hide resolved
/>
</NavigatorContext.Provider>
);
}

Expand Down Expand Up @@ -284,7 +287,7 @@ export function useBlocker(blocker: Blocker, when = true): void {
`useBlocker() may be used only in the context of a <Router> component.`
);

let navigator = React.useContext(LocationContext).navigator as Navigator;
let navigator = React.useContext(NavigatorContext);

React.useEffect(() => {
if (!when) return;
Expand Down Expand Up @@ -322,7 +325,7 @@ export function useHref(to: To): string {
`useHref() may be used only in the context of a <Router> component.`
);

let navigator = React.useContext(LocationContext).navigator as Navigator;
let navigator = React.useContext(NavigatorContext);
let path = useResolvedPath(to);

return navigator.createHref(path);
Expand Down Expand Up @@ -408,8 +411,7 @@ export function useNavigate(): NavigateFunction {
`useNavigate() may be used only in the context of a <Router> component.`
);

let locationContext = React.useContext(LocationContext);
let navigator = locationContext.navigator as Navigator;
let navigator = React.useContext(NavigatorContext);
let { pathname } = React.useContext(RouteContext);

let activeRef = React.useRef(false);
Expand Down