Skip to content

Commit

Permalink
chore: refine performance in useDateFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
vikiboss committed Sep 23, 2024
1 parent 5b940b2 commit 7383c57
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/react-use/src/use-date-format/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
}

0 comments on commit 7383c57

Please sign in to comment.