diff --git a/hooks/useCheckIfExternalLink.ts b/hooks/useCheckIfExternalLink.ts index 6e4b7a7ed..60476a4e8 100644 --- a/hooks/useCheckIfExternalLink.ts +++ b/hooks/useCheckIfExternalLink.ts @@ -1,9 +1,7 @@ -import { useEffect, useState } from "react" +import { useMemo } from "react" export const useCheckIfExternalLink = (url: string) => { - const [isExternal, setIsExternal] = useState(false) - - useEffect(() => { + return useMemo(() => { if (typeof window !== "undefined" && url?.length) { try { const hostName = window.location.hostname @@ -12,12 +10,12 @@ export const useCheckIfExternalLink = (url: string) => { const hostsMatch = hostName === newURL.hostname const langRouteRegex = /^\/[a-zA-Z]{2}\// - setIsExternal(!hostsMatch || !langRouteRegex.test(newURL.pathname)) + return !hostsMatch || !langRouteRegex.test(newURL.pathname) } catch { // Don't care. Expecting internal url (#, /my-pages/overview, etc) + return false } } + return false }, [url]) - - return isExternal }