Extract useIsClient
hook
This commit is contained in:
parent
b4e7d88ed8
commit
25fd852b55
|
@ -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>
|
||||
)
|
||||
}
|
||||
|
|
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