useMemo instead

This commit is contained in:
Linus Flood
2024-10-21 13:48:09 +02:00
parent 09777ee909
commit c816b9f802

View File

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