0
  ....
 const func1 = (intl) => {
  const text = intl.formatMessage({id: 'mesage'});
  const func2 = () => {
     return {
      <div>.....
      placeholder={text}</div>
   };
  };
    return {
   <div>.....
   {func2()}</div>
  };
 };

export default withStyles(styles)(injectIntl(component));`}

I'm trying to to injectIntl to translate strings using react-intl library, but I keep getting following error:

"Cannot read property 'formatMessage' of undefined"

I am trying to do something like this: React-Intl How to use FormattedMessage in input placeholder

user9081274
  • 33
  • 1
  • 6

3 Answers3

1

You Can Use This Way..!

The Simplest Solution For PlaceHolder Antd.

    import {injectIntl} from "react-intl";

    class DemoForm extends React.Component {
        render() {
            const {getFieldDecorator} = this.props.form;
            const {intl} = this.props;
            const placeholder = intl.formatMessage({id:'enterHere'});
            return (
                <Form.Item label={<FormattedMessage id='name'/>}>
                    {getFieldDecorator('name',)(<Input placeholder={placeholder}/>)}
                </Form.Item>
            )
       }
}
export const Demo = injectIntl(Form.create()(DemoForm));
Farrukh Malik
  • 746
  • 9
  • 13
0

You can use props.intl.formatMessage({id: 'mesage'});, intl object is included in props.

Further assuming Mycomponent is wrapped then :

const MyComponent = ({intl}) => { // we are extracting intl from prop object
// component code here
}
Umair Farooq
  • 1,763
  • 13
  • 16
0

I solved this issue by reimporting the intl in the class:

import { useIntl } from "react-intl";

const MyClass = (props) => {

    const intl = useIntl();
    return ( 
      ...
      {intl.formatMessage({ id: "message" })    // <==now it works}
      ...
    )
}
justadev
  • 1,168
  • 1
  • 17
  • 32