Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make formatMessage take in ReactElement #1367

Merged
merged 6 commits into from
Jul 12, 2019
Merged

feat: make formatMessage take in ReactElement #1367

merged 6 commits into from
Jul 12, 2019

Conversation

longlho
Copy link
Member

@longlho longlho commented Jul 12, 2019

Allow using formatMessage with ReactElement imperatively, e.g:

formatMessage('hello {world}', {world: <b>world</b>)
// returns ['hello ', <b>world</b>]

New signature will be:

formatMessage(msg: string, values: Record<string, string | number | null | undefined | React.ReactNode>): string[] | React.ReactNodeArray

Use case:

  • Right now the only way to format rich text is to use FormattedMessage, this brings feature-parity to the imperative version formatMessage as well.
  • formatMessage is also faster and doesn't create extra React Node.
  • Also remove the need to regen UUID for every rich text token in FormattedMessage, which improves perf as well
    Before:
100 x <FormattedMessage> with placeholder x 76.83 ops/sec ±9.83% (69 runs sampled)
100 x <FormattedMessage> with placeholder in AST form x 206 ops/sec ±1.12% (79 runs sampled)
100 x <FormattedMessage> with placeholder, cached x 573 ops/sec ±1.07% (84 runs sampled)
100 x <FormattedMessage> with placeholder, cached in AST form x 392 ops/sec ±4.28% (82 runs sampled)

After:

100 x <FormattedMessage> with placeholder x 85.51 ops/sec ±3.66% (66 runs sampled)
100 x <FormattedMessage> with placeholder in AST form x 210 ops/sec ±2.27% (76 runs sampled)
100 x <FormattedMessage> with placeholder, cached x 619 ops/sec ±2.00% (87 runs sampled)
100 x <FormattedMessage> with placeholder, cached in AST form x 426 ops/sec ±1.61% (84 runs sampled)

@redonkulus
Copy link
Member

What’s the use case for this?

@longlho longlho changed the title [WIP] feat: add formatMessageToParts [WIP] feat: make formatMessage take in ReactElement Jul 12, 2019
@longlho
Copy link
Member Author

longlho commented Jul 12, 2019

@redonkulus I updated the desc

@longlho longlho changed the title [WIP] feat: make formatMessage take in ReactElement feat: make formatMessage take in ReactElement Jul 12, 2019
@longlho longlho changed the title feat: make formatMessage take in ReactElement feat: make formatMessage take in ReactElement Jul 12, 2019
@longlho longlho merged commit 15ed625 into master Jul 12, 2019
@longlho longlho deleted the parts branch July 12, 2019 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants