[英]Using a string as input with reactDOM.render, marked.js and babel
So I am attempting to build a git markdown previewer using React, babel and marked.js. 所以我正在尝试使用React,babel和marked.js构建一个git markdown预览器。 Marked.js is a markdown parser.
Marked.js是一个markdown解析器。
So say I have a value that I want to use from my html. 因此,说我有一个要从我的html使用的值。
<div id="markdownInput"># This is my input</div>
In javascript, I store it as the variable myInput. 在javascript中,我将其存储为变量myInput。
var myInput = document.getElementById("markdownInput").value;
myInput is now "# This is my input" and is a string. myInput现在是“#这是我的输入”,是一个字符串。 Marked.js uses a function called "marked()" that takes a string as input and converts the git markdown to html.
Marked.js使用名为“ marked()”的函数,该函数将字符串作为输入并将git markdown转换为html。
var myOutput = marked(myInput);
myOutput is now equal to: myOutput现在等于:
"<h1>This is my input</h1>"
Notice that the markdown is converted (# =>h1) but it is also stored as a string. 请注意,降价已转换(#=> h1),但它也存储为字符串。 If I attempt to use this with reactDOM.render like so:
如果我尝试将其与reactDOM.render像这样使用:
ReactDOM.render(myOutput, document.getElementById("output-container"));
My "output-container" in my HTML ends up with displaying: 我在HTML中的“输出容器”最终显示为:
<h1>This is my input</h1>
rather than displaying " This is my input " in large header letters. 而不是使用大标题字母显示“ 这是我的输入 ”。 What it boils down to is that React.DOM.render() needs an object as input and not a string.
归结为React.DOM.render()需要一个对象作为输入,而不是字符串。 It needs:
它需要:
<h1>This is my input</h1>
As an object but I am inputting it as a string because the marked() function only outputs as a string (which wraps everything in quotes). 作为一个对象,但我将其输入为字符串,因为marked()函数仅输出为字符串(将所有内容都用引号引起来)。
Is there anything that I can do to convert myOutput to an object? 我可以做些什么将myOutput转换为对象吗? I am extremely knew to React so perhaps I haven't learned a better method for something like this.
我对React非常了解,所以也许我还没有为这种事情学到更好的方法。
Thanks 谢谢
Well, found this Using marked in react and used the "dangerouslySetInnerHTML" attribute. 好吧, 在React中找到了Using标记并使用了“ dangerouslySetInnerHTML”属性。 Is there perhaps a better way to do this though?
不过,也许有更好的方法吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.