import {format, formatDistance, formatRelative, subDays} from 'date-fns' import * as React from 'react' import {combineLatest, timer} from 'rxjs' import {map, share, take} from 'rxjs/operators' import {withPropsStream} from '../../withPropsStream' const UPDATE_INTERVAL = 1000 const currentTime$ = timer(0, UPDATE_INTERVAL).pipe( take(10), map(() => new Date()), share() ) interface OwnerProps { time: Date includeSeconds?: boolean } interface ChildProps extends OwnerProps { relativeTime: string } const TimeDistance = withPropsStream( ownerProps$ => combineLatest(currentTime$, ownerProps$).pipe( map(([currentTime, ownerProps]) => ({ ...ownerProps, relativeTime: formatDistance(ownerProps.time, currentTime, { includeSeconds: ownerProps.includeSeconds }) })) ), props => <>{props.relativeTime} ) const NOW = new Date() export const PassThroughPropsExample = () => ( <>

With includeSeconds true

With synchronized updates

Page loaded ago

Page loaded ago

Without includeSeconds

Page loaded ago

)