Skip to content

Commit

Permalink
refactor(use-next-link): adopt useComponentWillReceiveUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Nov 25, 2024
1 parent bc7a951 commit 3f1511d
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions src/use-next-link/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import 'client-only';

import type { LinkProps } from 'next/link';
import { useEffect, useMemo, useState, useTransition, useCallback } from 'react';
import { useEffect, useMemo, useTransition, useCallback } from 'react';
import { useRouter } from 'next/navigation';

import { formatUrl } from 'next/dist/shared/lib/router/utils/format-url';
import { useIntersection } from '../use-intersection';
import { useComponentWillReceiveUpdate } from '../use-component-will-receive-update';

import type {
PrefetchOptions as AppRouterPrefetchOptions
Expand Down Expand Up @@ -118,15 +119,7 @@ function useNextLink(hrefProp: string | UrlObject,
}), []));

const resolvedHref = useMemo(() => (typeof hrefProp === 'string' ? hrefProp : formatUrl(hrefProp)), [hrefProp]);
const [previousResolvedHref, setPreviousResolvedHref] = useState<string>(resolvedHref);

if (previousResolvedHref !== resolvedHref) {
// It is safe to set the state during render, as long as it won't trigger an infinite render loop.
// React will render the component with the current state, then throws away the render result
// and immediately re-executes the component function with the updated state.
setPreviousResolvedHref(resolvedHref);
resetVisible();
}
useComponentWillReceiveUpdate(resetVisible, [resolvedHref]);

// Prefetch the URL if we haven't already and it's visible.
useEffect(() => {
Expand Down

0 comments on commit 3f1511d

Please sign in to comment.