0

我有一个在树(又名门户)之外呈现的组件(弹出窗口),因此丢失了以下内容IntlProvider

<IntlProvider locale="en">
    <App />
</IntlProvider>
// * <- renders here

Redux 通过允许将 store 实例作为组件的 prop 传递来解决相同的问题:

import store from "./store";

const Component = () => <Popup store={store} />;

有没有类似的方法可以用于 ReactIntl​​?

4

2 回答 2

0

您可以将injectIntl​​ HOC 与注入intl组件一起使用。

只需将您的代码更改为:

const Component = () => <Popup ... />

至:

import { injectIntl  } from 'react-intl';
const Component = ({ intl }) => <Popup intl={intl} />
export default injectIntl(Component)
于 2018-01-29T13:44:57.073 回答
-1

如果您不想将组件包装到 HOC 中,useIntl 钩子也很好:)

于 2020-06-10T09:03:08.547 回答