React-intl for non components

后端 未结 5 2164
醉酒成梦
醉酒成梦 2021-02-05 17:14

Currently I have the following code to expose react-intl to non-components, but it throws an error for intl as undefined.

I have created a separate component as \'Curren

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-05 17:39

    This line: const { intl } = this.context.intl; should be const { intl } = this.context;

    Here is a reference post of someone doing almost the exact same thing as you are: https://github.com/yahoo/react-intl/issues/983#issuecomment-342314143

    In the above the author is creating essentially a singleton that is exported instead of creating a new instance each time like you have above. This might be something you want to consider as well.

提交回复
热议问题