UNPKG

@dr.pogodin/react-utils

Version:

Collection of generic ReactJS components and utils

41 lines (40 loc) 1.7 kB
import dayjs from 'dayjs'; import { timer } from '@dr.pogodin/js-utils'; /** * This react hook wraps Date.now() function in a SSR friendly way, * ensuring that all calls to useCurrent() within the same render return * exactly the same time (which is retrieved from Date.now() first, and * then stored in the global state to be reused in all other calls), which * is also passed and used in the first client side render, and then updated * with a finite precision to avoid infinite re-rendering loops. */ export declare function useCurrent({ autorefresh, globalStatePath, precision, }?: { autorefresh?: boolean | undefined; globalStatePath?: string | undefined; precision?: number | undefined; }): number; /** * Wraps the standard Date.getTimezoneOffset() method in a SSR-friendly way. * This hook retrieves the offset value at the client side and uses a cookie * to pass it to the server in subsequent requests from that user. At the server * side the value from cookie is used in renders and passed back to the client * via the global state. Prior to the value being known (in the very first * request from the user, when the cookie is still missing), zero value is used * as the default value. */ export declare function useTimezoneOffset({ cookieName, globalStatePath, }?: { cookieName?: string | undefined; globalStatePath?: string | undefined; }): number; declare const _default: typeof dayjs & { DAY_MS: number; HOUR_MS: number; MIN_MS: number; SEC_MS: number; YEAR_MS: number; now: () => number; timer: typeof timer; useCurrent: typeof useCurrent; useTimezoneOffset: typeof useTimezoneOffset; }; export default _default;