【发布时间】:2017-04-08 15:56:15
【问题描述】:
我想使用react-intl API 的formatMessage 函数插入一条消息作为占位符,但我不知道访问该函数的正确方法。
这是我所拥有的简化版本:
// index.tsx
<IntlProvider locale={`fr-FR`} messages={messages_fr}>
<NameForm/>
</IntlProvider>
// nameForm.tsx
interface NameFormProps {
intl?: InjectedIntlProps,
}
export default injectIntl(NameForm);
class NameForm extends React.Component<NameFormProps, {}> {
render() {
let namePlaceholder = this.props.intl.formatMessage({
id: "NAME_PLACEHOLDER",
defaultMessage: "name"
});
return (
<form>
<input placeholder={namePlaceholder} type="text"/>
</form>
);
}
}
我使用InjectedIntlProps 作为intl 属性的类型,因为IntlShape 似乎没有提供formatMessage 方法。
我添加了一个?到intl 道具,因为我一直有一个Property 'intl' is missing(但injectIntl 不应该返回一个没有这个道具的组件吗?)
现在它可以编译了,但是运行时出现错误Cannot read property 'displayName' of undefined。我猜这是因为默认导出没有明确的名称)。
我觉得我的方向不对,但找不到 typescript/react-intl 项目的任何示例。
感谢您的帮助!
【问题讨论】:
标签: typescript react-intl