From 7383c57bdd37b408cf42838467c162ad9e5c420c Mon Sep 17 00:00:00 2001 From: Viki Date: Mon, 23 Sep 2024 16:59:44 +0800 Subject: [PATCH] chore: refine performance in `useDateFormat` --- packages/react-use/src/use-date-format/index.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/react-use/src/use-date-format/index.ts b/packages/react-use/src/use-date-format/index.ts index 39145f7..2f7cad6 100644 --- a/packages/react-use/src/use-date-format/index.ts +++ b/packages/react-use/src/use-date-format/index.ts @@ -1,4 +1,5 @@ import { useCreation } from '../use-creation' +import { useLatest } from '../use-latest' import { formatDate, normalizeDate } from './format-date' export { formatDate, normalizeDate } from './format-date' @@ -38,13 +39,14 @@ export function useDateFormat( */ options: UseDateFormatOptions = {}, ): string { - const { fallback: fallbackStr = '' } = options + const { fallback: fallbackStr = '', unicodeSymbols, locales } = options + const latest = useLatest({ options }) const result = useCreation(() => { const nDate = normalizeDate(date) const isInvalid = Number.isNaN(nDate.getTime()) - return isInvalid ? fallbackStr : formatDate(nDate, formatStr, options) - }, [date, formatStr, fallbackStr, options]) + return isInvalid ? fallbackStr : formatDate(nDate, formatStr, latest.current.options) + }, [date, formatStr, fallbackStr, unicodeSymbols, locales]) return result }