Sanity Library Reference Docs
    Preparing search index...

    Function useUnitFormatter

    • Returns a formatter with the given options. Function takes a number and the unit to format as the second argument. The formatter will yield localized output, based on the users' selected locale.

      This differs from regular Intl.NumberFormat in two ways:

      1. You do not need to instantiate a new formatter for each unit you want to format (still happens behind the scenes, but is memoized)
      2. The default unit display style (unitDisplay) is long

      Parameters

      Returns UnitFormatter

      Formatter function

      function MyComponent() {
      const format = useUnitFormatter()
      return <div>{format(2313, 'meter')}</div>
      // en-US -> 2,313 meters
      // fr-FR -> 2 313 mètres
      }