Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs React Intl-无法读取未定义的属性“formatMessage”_Reactjs_React Redux_React Intl - Fatal编程技术网

Reactjs React Intl-无法读取未定义的属性“formatMessage”

Reactjs React Intl-无法读取未定义的属性“formatMessage”,reactjs,react-redux,react-intl,Reactjs,React Redux,React Intl,使用StylesStylesInjectionLComponent导出默认值;`} 我正在尝试使用react intl库injectIntl来翻译字符串,但一直出现以下错误: 无法读取未定义的属性“formatMessage” 我正在尝试这样做: 您可以使用props.intl.formatMessage{id:'mesage'};,道具中包含intl对象 进一步假设Mycomponent已包装,则: .... const func1 = (intl) => { const te

使用StylesStylesInjectionLComponent导出默认值;`}

我正在尝试使用react intl库injectIntl来翻译字符串,但一直出现以下错误:

无法读取未定义的属性“formatMessage”

我正在尝试这样做:

您可以使用props.intl.formatMessage{id:'mesage'};,道具中包含intl对象

进一步假设Mycomponent已包装,则:

  ....
 const func1 = (intl) => {
  const text = intl.formatMessage({id: 'mesage'});
  const func2 = () => {
     return {
      <div>.....
      placeholder={text}</div>
   };
  };
    return {
   <div>.....
   {func2()}</div>
  };
 };
你可以用这种方式

占位符Antd的最简单解决方案


如果您的问题得到了回答,请接受或投票,感谢它可以工作,但给出了一个警告错误:必须使用销毁道具分配,然后您可以使用const{intl}=props;,取决于组件是否为类组件,然后const{intl}=this.props;然后使用intl.formatMessage{id:'mesage'}`好吧,我想这起作用了。我不能投票给你,因为我的反馈很低。非常感谢您,我有另一个函数MyComponent.defaultprops,它要求我执行相同的操作,但它位于MyComponent之外。在这种情况下,如何翻译字符串?
const MyComponent = ({intl}) => { // we are extracting intl from prop object
// component code here
}
    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));