Skip to content

Commit

Permalink
Improve navigation api
Browse files Browse the repository at this point in the history
  • Loading branch information
serg-plusplus committed Jun 27, 2024
1 parent 1cbb162 commit 34fd00b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
5 changes: 2 additions & 3 deletions src/lib/navigation/Redirect.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { FC, ReactElement, useLayoutEffect } from "react";
import { changeState } from "lib/history";

import { Destination } from "./types";
import { toHash, toURL } from "./utils";
import { navigate } from "./navigate";

type RedirectProps = {
to: Destination;
Expand All @@ -18,7 +17,7 @@ const Redirect: FC<RedirectProps> = ({
fallback = null,
}) => {
useLayoutEffect(
() => changeState(toURL(toHash(to, merge)), !push),
() => navigate(to, { merge, replace: !push }),
[to, merge, push],
);

Expand Down
12 changes: 9 additions & 3 deletions src/lib/navigation/navigate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import { changeState } from "lib/history";
import { Destination } from "./types";
import { toHash, toURL } from "./utils";

export function navigate(to: Destination, action?: "replace") {
const url = toURL(toHash(to));
changeState(url, action === "replace");
export function navigate(
to: Destination,
opts: {
replace?: boolean;
merge?: boolean | string[];
} = {},
) {
const url = toURL(toHash(to, opts.merge));
changeState(url, opts.replace);
}

0 comments on commit 34fd00b

Please sign in to comment.