reactjs - React-intl const <FormattedMessage/> 给出 [object object] 作为结果

标签 reactjs react-intl

我正在使用 "react-intl": "^2.4.0" , 和 "react": "^16.2.0" .
我正在尝试分配 <FormattedMessage/> 的输出标记为常量并将其放在输入标记占位符中。
Code Image
预期的输出输入字段,其中包含相应语言的文本,但它在文本框中出现 [object object]。

最佳答案

<FormattedMessage />是无法放置到 placeholder 的组件它需要一个原始字符串。

import {injectIntl} from 'react-intl'; 

class TestComponent extends React.Component{
  render(){
    const { intl } = this.props;
    return (
        <input placeholder={intl.formatMessage({ id: "loginPage.username", defaultMessage: 'username'})}/>
    )
  }
}

export default injectIntl(TestComponent);

关于reactjs - React-intl const <FormattedMessage/> 给出 [object object] 作为结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52423291/

相关文章:

javascript - 如何使用 FormattedMessage 将对象转换为字符串以使用 REACTJs 在 d3.js 图表中呈现标签?

javascript - 如何在 React 和 Typescript 中使用 useRef 钩子(Hook)和 getClientBoundingRect?

javascript - 需要加入x轴和y轴标签highcharts

react-intl - babel-plugin-react-intl : Extract strings to a single file

javascript - React 修改显示的值而不改变输入表单的值

reactjs - 如何一起使用 react-intl 和 typescript 中的 injectIntl​​?

reactjs - react-i18next 并用组件替换占位符键

javascript - 类型错误 : Cannot read property 'map' of undefined in ProductList. js

reactjs - 在react-select中自定义 `No data found`验证消息

javascript - map() 返回不在 react 组件中呈现