T
TanStack2y ago
deep-jade

Determine if a navigation would go to the previous page.

Hey! I am trying to wrap the Link component such that if the link would cause a navigation to what was the previous page, instead it will run history.back(). My idea is to store the last location locally, figure out what the next location will be and then either return the router Link or do a history back. I've got as far as this:
import { Link, LinkComponent, useRouter } from "@tanstack/react-router";
import { ReactNode, useMemo } from "react";

export const ThreadLink: LinkComponent<"a"> = (props) => {
const goBack = useMemo(() => {
// TODO: decide if we are actually going back
return false;
}, []);

if (goBack) {
return (
<div className="cursor-pointer" onClick={() => history.back()}>
{props.children as ReactNode}
</div>
);
} else {
return <Link {...props} />;
}
};
import { Link, LinkComponent, useRouter } from "@tanstack/react-router";
import { ReactNode, useMemo } from "react";

export const ThreadLink: LinkComponent<"a"> = (props) => {
const goBack = useMemo(() => {
// TODO: decide if we are actually going back
return false;
}, []);

if (goBack) {
return (
<div className="cursor-pointer" onClick={() => history.back()}>
{props.children as ReactNode}
</div>
);
} else {
return <Link {...props} />;
}
};
but in order to implement goBack I need to somehow calculate the location from the props that will be passed to Link without Link actually being clicked. I've looked through the router API, but can't figure out how to do do this. Is there a function somewhere to help me, or is there a better way to achieve this?
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?