React-intl for non components

后端 未结 5 2040
醉酒成梦
醉酒成梦 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:35

    If you can accept to use a function component I prefer to use the useIntl hook

    https://reactjs.org/docs/components-and-props.html#function-and-class-components

    I can then get values like this:

    import { useIntl } from "react-intl";
    
    const intl = useIntl()
    intl.formatMessage({ id: 'myId' }),
    

    https://formatjs.io/docs/react-intl/api/#useintl-hook

提交回复
热议问题