如何在不使用FormattedMessage的情况下在ReactIntl 2.0中检索字符串 [英] How to retrieve a string in ReactIntl 2.0 without using FormattedMessage
问题描述
如果我错了,请纠正我,ReactIntl中的FormattedMessage返回一个由span标记包装的字符串.在ReactIntl 1.2中,我们可以选择使用this.getIntlMessage('key')
仅获取字符串部分.
Please correct me if I am wrong, FormattedMessage in ReactIntl returns a string wrapped by span tags. In ReactIntl 1.2, we have the option to use this.getIntlMessage('key')
to get only the string part.
这是我的问题:ReactIntl 2.0中是否有与之等效的东西?我知道可以通过在
Here is my question: Is there an equivalent of that in ReactIntl 2.0? I am aware that the string can be obtained by using the Function-As-Child pattern in FormattedMessage as
<FormattedMessage id="placeholder">
{(formattedValue)=>(
<MyComponent ref="mycomponent" placeholder={formattedValue}/>
)}
</FormattedMessage>
但是,它把我的组件中的"ref"弄乱了,我再也无法使用this.refs.mycomponent
来访问该组件.
However, it messes up the 'ref' in my component and I can't access to the component using this.refs.mycomponent
any more.
推荐答案
好的,可以解决此问题.我可以这样添加 ReactIntl 作为组件中的上下文:
Ok, there is a work around for that. I can add ReactIntl as the context in the component like this:
contextTypes: {
intl: React.PropTypes.object.isRequired,
},
然后,当尝试检索消息的字符串并使用它时(例如在占位符中),我可以执行此操作.
Then when trying to retrieve the string of the message and use it, for example in a placeholder, I can do this.
<MyComponent ref="mycomponent" placeholder={this.context.intl.messages.placeholder}/>
这篇关于如何在不使用FormattedMessage的情况下在ReactIntl 2.0中检索字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!