Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

I'm wondering what is the right way to define FormatNumberOptions that depends on formatted value. Now I'm using a function that takes number value and returns FormatNumberOptions object depends on this value and defined format, and it basically has a logic like this:

    ...
    else if (absValue > 10 && absValue <= 1_000) {
      if (defaultOpts.format === 'compactLong') {
        defaultOpts.minimumFractionDigits = 0;
        defaultOpts.maximumFractionDigits = 1;
      }
      if (defaultOpts.format === 'precision') {
        defaultOpts.maximumFractionDigits = 2;
      }
    } else if (absValue > 1_000 && absValue <= 1_000_000) {
      if (defaultOpts.format === 'compactLong') {
        defaultOpts.minimumFractionDigits = 1;
        defaultOpts.maximumFractionDigits = 1;
      }
      if (defaultOpts.format === 'precision') {
        defaultOpts.maximumFractionDigits = 0;
      }
    } else 
    ...

And my IntlConfig has formats are:

const intlFormats: CustomFormats = {
  number: {
    highPrecision: {
      notation: 'standard',
      maximumFractionDigits: 12,
    },
    precision: {
      notation: 'standard',
      maximumFractionDigits: 4,
    },
    compactLong: {
      notation: 'compact',
      compactDisplay: 'short',
      trailingZeroDisplay: 'auto',
    },
  },
};

It works more or less ok, but it feels like an overkill and I assume it might cause some performance degradation in comparison of using one format for all the values (correct me if i'm wrong but it seems that react will recreate component in this case?), so i'd like to know if there is a better way to do this?

And when I use this setup with https://github.com/barvian/number-flow library, it breaks animation on thresholds like when value changes from 1000 to 1100, most probably because format changes and react recreates and remounts component instead of rerendering it. FWIW, not exactly the same (there is another reason of remount), but kind of related discussion is here: barvian/number-flow#112.

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
1 participant
Morty Proxy This is a proxified and sanitized view of the page, visit original site.