Extract useIsClient
hook
This commit is contained in:
parent
b4e7d88ed8
commit
25fd852b55
|
@ -1,22 +1,16 @@
|
||||||
import { DateTimeTooltip } from './datetime-tooltip'
|
import { DateTimeTooltip } from './datetime-tooltip'
|
||||||
import React, { useEffect, useState } from 'react'
|
|
||||||
import { fromNow } from 'web/lib/util/time'
|
import { fromNow } from 'web/lib/util/time'
|
||||||
|
import { useIsClient } from 'web/hooks/use-is-client'
|
||||||
|
|
||||||
export function RelativeTimestamp(props: { time: number }) {
|
export function RelativeTimestamp(props: { time: number }) {
|
||||||
const { time } = props
|
const { time } = props
|
||||||
const [isClient, setIsClient] = useState(false)
|
const isClient = useIsClient()
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
// Only render on client to prevent difference from server.
|
|
||||||
setIsClient(true)
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DateTimeTooltip
|
<DateTimeTooltip
|
||||||
className="ml-1 whitespace-nowrap text-gray-400"
|
className="ml-1 whitespace-nowrap text-gray-400"
|
||||||
time={time}
|
time={time}
|
||||||
>
|
>
|
||||||
{isClient ? fromNow(time) : ''}
|
{isClient && fromNow(time)}
|
||||||
</DateTimeTooltip>
|
</DateTimeTooltip>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
7
web/hooks/use-is-client.ts
Normal file
7
web/hooks/use-is-client.ts
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
import { useEffect, useState } from 'react'
|
||||||
|
|
||||||
|
export const useIsClient = () => {
|
||||||
|
const [isClient, setIsClient] = useState(false)
|
||||||
|
useEffect(() => setIsClient(true), [])
|
||||||
|
return isClient
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user