如何在react.js渲染中显示javascript/html代码。
我已经尝试过这样的
var thisIsMyCopy = '<p>copy copy copy <strong>strong copy</strong </p>';
return (
<div>
<div dangerouslySetInnerHTML={{__html: thisIsMyCopy}}>
</div>
</div>
)
这是 html 代码..但我也想在屏幕上显示 javascript 代码...我该怎么做.. 你能指导我......如何继续
最佳答案
它已经是字符串版本,只需要在花括号内包含变量即可。 dangerouslySetInnerHTML
适用于要将 html 字符串转换为实际 html 的情况。
var thisIsMyCopy = '<p>copy copy copy <strong>strong copy</strong </p>';
return (
<div>
{thisIsMyCopy}
</div>
)
如果你想要一个函数的字符串版本,只需使用任何函数并向其中添加一个字符串,如下所示:
function functionString() { console.log('stringed function") }
var functionAsString = '' + functionString
return (
<div>
{functionAsString}
</div>
)
这是一支工作笔,演示了这一点 http://codepen.io/finalfreq/pen/JRqEWW
关于javascript - 在react.js中像纯文本一样显示javascript/html代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40293576/