javascript React 不断在属性中转义 amp 字符 (&)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29907127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
React keeps escaping the amp character (&) in attributes
提问by Taig
I have a component with a data-icon
attribute. The value of this attribute should be, for instance, 
so that css can render it via content: attr( data-icon );
.
我有一个带有data-icon
属性的组件。例如,这个属性的值应该是,
以便 css 可以通过content: attr( data-icon );
.
However, whatever I try: React keeps escaping to &
. Even when I provide the proper unicode character \u0026#xf00f
.
但是,无论我尝试什么:React 一直转义到&
. 即使我提供了正确的 unicode 字符\u0026#xf00f
。
Is there some way to stop React from messing with that value? Besides dangerously setting inner html, as I do not want to add another wrapper.
有什么方法可以阻止 React 破坏该值?除了危险地设置内部 html,因为我不想添加另一个包装器。
Component
零件
define( [ 'react', 'util' ], function( React, Util )
{
return React.createClass(
{
render: function()
{
//var amp = '\u0026',
var amp = String.fromCharCode( 38 ),
// Util.icons[x] returns a String, such as "f00f"
code = amp + '#x' + Util.icons[this.props.name] + ';';
return (
<i data-icon={code}>
{this.props.children ? <span>{this.props.children}</span> : null}
</i>
);
}
} );
} );
Usage
用法
<Widget.Icon name="add" />
Output
输出
<i data-icon="&#xf0fb;" data-reactid=".170lse36465.7.0"></i>
回答by Taig
Well, I just realized, that for my particular use case I can simply get away with:
好吧,我刚刚意识到,对于我的特定用例,我可以简单地逃脱:
<i data-icon={String.fromCharCode( "f00f" )} />