reactjs 如何在 JSX 中使用转义字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48484392/
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
how to use escape characters in JSX
提问by aashir khan
I want to use some escape characters in my React application but I have not found any resource that how can I use escape character in my application.
我想在我的 React 应用程序中使用一些转义字符,但我还没有找到任何关于如何在我的应用程序中使用转义字符的资源。
Is any methodology available?
有没有可用的方法论?
回答by rjdkolb
Use the sameescape as Javascript (ECMAScript)
使用与Javascript (ECMAScript)相同的转义
\' single quote
\' 单引号
\" double quote
\" 双引号
\ backslash
\ 反斜杠
\n new line
\n 换行
\r carriage return
\r 回车
\t tab
\t 标签
\b backspace
\b 退格
\f form feed
\f 换页
For the HTML portion, use HTML escape characters.
对于 HTML 部分,使用 HTML 转义字符。
There are some minor gotchas,to be aware of like evaluating escape chars between { }
有一些轻微的陷阱,要知道像评估之间逃逸字符{}
HTML:
HTML:
<div id="container">
<!-- This element's contents will be replaced with MyComponent. -->
</div>
JSX:
JSX:
class MyComponent extends React.Component {
render() {
console.info('Test line\nbreak');
return <div>Hello {this.props.name} <> </div>;
}
}
ReactDOM.render(
<MyComponent name="Stackoverflow < !-- comment in name -->" />,
document.getElementById('container')
);
This program prints this to the console:
该程序将其打印到控制台:
Test line
break
Add the user's screen is the following:
添加用户的画面如下:
Hello Stackoverflow < !-- comment in name --> <>
你好 Stackoverflow <!-- 名称中的注释 --> <>

