Skip to content

Commit 5584a27

Browse files
committed
Fix ContentLink hot reload issue by stabilizing onNavigateTo callback
Prevents controller recreation when onNavigateTo callback reference changes during Next.js hot reloads. The callback is now stored in a ref, so the controller only recreates when enabled or root changes intentionally.
1 parent ed177a6 commit 5584a27

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

src/useContentLink/index.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,16 @@ export function useContentLink(
6464
const { enabled = true, onNavigateTo, root } = options;
6565

6666
const controllerRef = useRef<Controller | null>(null);
67+
// Store the callback in a ref to avoid recreating the controller when it changes
68+
const onNavigateToRef = useRef(onNavigateTo);
6769

68-
// Create/dispose controller based on enabled flag and dependencies
70+
// Keep the callback ref up to date
71+
useEffect(() => {
72+
onNavigateToRef.current = onNavigateTo;
73+
}, [onNavigateTo]);
74+
75+
// Create/dispose controller based on enabled flag and root only
76+
// The onNavigateTo callback is accessed via ref, so changes don't trigger recreation
6977
useEffect(() => {
7078
if (!enabled) {
7179
if (controllerRef.current) {
@@ -76,7 +84,7 @@ export function useContentLink(
7684
}
7785

7886
const controller = createController({
79-
onNavigateTo,
87+
onNavigateTo: (path: string) => onNavigateToRef.current?.(path),
8088
root: root?.current || undefined,
8189
});
8290

@@ -86,7 +94,7 @@ export function useContentLink(
8694
controller.dispose();
8795
controllerRef.current = null;
8896
};
89-
}, [enabled, onNavigateTo, root]);
97+
}, [enabled, root]);
9098

9199
// Stable method references that call through to the controller
92100
const enableClickToEdit = useCallback(

0 commit comments

Comments
 (0)