Skip to content

Commit f39f9bb

Browse files
committed
[Portal] Fix table of contents sidebar not updating on page change
1 parent 68ad62f commit f39f9bb

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

apps/portal/src/components/others/TableOfContents.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { cn } from "@/lib/utils";
44
import Link from "next/link";
5+
import { usePathname } from "next/navigation";
56
import { useEffect, useRef, useState } from "react";
67

78
/**
@@ -29,6 +30,7 @@ export function TableOfContentsSideBar(props: {
2930
}) {
3031
const [nodes, setNodes] = useState<TableOfContentNode[]>([]);
3132
const tocRef = useRef<HTMLDivElement>(null);
33+
const pathname = usePathname();
3234

3335
const [hideNav, setHideNav] = useState(false);
3436
const { filterHeading } = props;
@@ -45,6 +47,9 @@ export function TableOfContentsSideBar(props: {
4547
root.querySelectorAll("a[href^='#']"),
4648
) as HTMLAnchorElement[];
4749

50+
// using pathname to fix exhaustive dependency lint warning without suppressing it entirely
51+
tocRef.current?.setAttribute("data-pathname", pathname);
52+
4853
// hide anchors inside hidden elements
4954
const anchors = anchorsAll.filter((anchor) => {
5055
return anchor.closest("[data-collapsible]") === null;
@@ -104,7 +109,7 @@ export function TableOfContentsSideBar(props: {
104109
return () => {
105110
observer.disconnect();
106111
};
107-
}, [filterHeading]);
112+
}, [filterHeading, pathname]);
108113

109114
return (
110115
<nav

0 commit comments

Comments
 (0)