Skip to content

Commit

Permalink
fix: add default value and check before setting href (#2495)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomiir authored Jul 2, 2024
1 parent c07a89b commit 5e0ab64
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions apps/laboratory/src/components/RandomLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
import Link from 'next/link'
import { useState, useEffect } from 'react'

export function RandomLink({ hrefs, children }: { hrefs: string[]; children: React.ReactNode }) {
export function RandomLink({
hrefs = [],
children
}: {
hrefs: string[]
children: React.ReactNode
}) {
const [href, setHref] = useState<string>()
useEffect(() => {
const newHref = hrefs[Math.floor(Math.random() * hrefs.length)]
setHref(newHref)
if (newHref) {
setHref(newHref)
}
}, [hrefs])

return href ? <Link href={href}>{children}</Link> : <></>
Expand Down

0 comments on commit 5e0ab64

Please sign in to comment.