从 react-intl 获取字符串 <FormattedNumber>

getting String from react-intl's <FormattedNumber>

我有一个 FormattedNumber 组件,如下所示:

<FormattedNumber
  style="percent"
  maximumFractionDigits={2}
  value={0.555}
>
</FormattedNumber>

还有一个以字符串作为输入的函数,我需要将 FormattedNumber 的结果传递给该函数,我尝试使用它来获取数字:

const num = ReactDOMServer.renderToString(
  <FormattedNumber
  style="percent"
  maximumFractionDigits={2}
  value={0.543}
  >
  </FormattedNumber>
)

但失败并出现错误 Unhandled Rejection (Error): [React Intl] Could not find required intl object. <IntlProvider> needs to exist in the component ancestry.

我需要一种将 FormattedNumber 转换为字符串的方法,它必须与百分比、货币等一起使用。

因为您需要将您的组件包装到 IntlProvider 组件中。

Check the React-Intl doc