Extract useIsClient hook

This commit is contained in:
Marshall Polaris 2022-10-12 01:04:52 -07:00
parent b4e7d88ed8
commit 25fd852b55
2 changed files with 10 additions and 9 deletions

View File

@ -1,22 +1,16 @@
import { DateTimeTooltip } from './datetime-tooltip'
import React, { useEffect, useState } from 'react'
import { fromNow } from 'web/lib/util/time'
import { useIsClient } from 'web/hooks/use-is-client'
export function RelativeTimestamp(props: { time: number }) {
const { time } = props
const [isClient, setIsClient] = useState(false)
useEffect(() => {
// Only render on client to prevent difference from server.
setIsClient(true)
}, [])
const isClient = useIsClient()
return (
<DateTimeTooltip
className="ml-1 whitespace-nowrap text-gray-400"
time={time}
>
{isClient ? fromNow(time) : ''}
{isClient && fromNow(time)}
</DateTimeTooltip>
)
}

View File

@ -0,0 +1,7 @@
import { useEffect, useState } from 'react'
export const useIsClient = () => {
const [isClient, setIsClient] = useState(false)
useEffect(() => setIsClient(true), [])
return isClient
}