[英]Why is React.js not loading HTML code when using marked library
I am trying to build a markdown editor for which I am using the Marked library My Code doesn't show any error but doesn't render the html at all. 我试图建立一个Markdown编辑器,我正在使用Marked库 My Code不会显示任何错误,但根本不会呈现html。
Code : 代码:
class TextArea extends React.Component { render() { return ( <div dangerouslySetInnerHTML={{__html: this.props.value}} /> ); } } class Markdown extends React.Component { constructor(props) { super(props); this.state = { value: " " }; this.handleChange = this.handleChange.bind(this); } handleChange(event) { this.setState({ value: event.target.value }); } render() { return ( <div> <textarea onChange={this.handleChange} /> <TextArea value={marked(this.state.value,{sanitize: true})} /> </div> ); } } ReactDOM.render(<Markdown />, document.getElementById("markdown"));
The input I have given to the code is in markdown format 我提供给代码的输入为markdown格式
Heading ======= Sub-heading ----------- ### Another deeper heading Paragraphs are separated by a blank line. Leave 2 spaces at the end of a line to do a line break Text attributes *italic*, **bold**, `monospace`, ~~strikethrough~~ . Shopping list: * apples * oranges * pears Numbered list: 1. apples 2. oranges 3. pears The rain---not the reign---in Spain.
<h1 id="heading">Heading</h1> <h2 id="sub-heading">Sub-heading</h2> <h3 id="another-deeper-heading">Another deeper heading</h3> <p>Paragraphs are separated by a blank line.</p> <p>Leave 2 spaces at the end of a line to do a<br>line break</p> <p>Text attributes <em>italic</em>, <strong>bold</strong>, <code>monospace</code>, <del>strikethrough</del> .</p> <p>Shopping list:</p> <ul> <li>apples</li> <li>oranges</li> <li><p>pears Numbered list:</p> </li> <li><p>apples</p> </li> <li>oranges</li> <li>pears</li> </ul> <p>The rain---not the reign---in Spain.</p>
But It doesn't render the HTML Please Help me. 但是它不会呈现HTML请帮助我。 Thanks in advance
提前致谢
this.state.value
should be this.props.value
because you are sending value
in as a prop of TextArea
: this.state.value
应该是this.props.value
因为您正在将value
作为TextArea
的prop发送:
class TextArea extends React.Component {
render() {
return (
<div dangerouslySetInnerHTML={{__html: this.props.value}} />
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.