简体   繁体   English

为什么使用标记库时React.js不加载HTML代码

[英]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. 
and the output give by marked library is 标记库给出的输出是

 <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.

相关问题 为什么React在使用标记库时返回“Object()不是函数”? - Why React returns “Object() is not a function” when using marked library? React.js:将React.js代码与HTML分开 - React.js: Separate React.js code from HTML 在React.js中包装HTML时避免使用危险地使用SetInnerHTML - Avoid using dangerouslySetInnerHTML when wrapping HTML in React.js 为什么在使用 redux 和 react.js 时我的道具“未定义”? - Why are my props `undefined` when using redux and react.js? React.js 中的条件加载 - Conditional loading in React.js 当我导航到主包未加载的嵌套路由时使用 React.js 延迟加载 - Using React.js lazy loading when I navigate to a nested route the main bundle doesn't load 使用React.js时的一般结构 - General structure when using React.js 在React.js中加载异步数据时防止UI闪烁 - Preventing UI flicker when loading async data in React.js 在 React.js 中,何时使用 `${}` 将 Javascript 代码注入 JSX,我们何时使用常规 {}? - In React.js when do inject Javascript code into JSX using `${}` and when do we use regular {}? React.js to pain HTML: how to convert imageupload.html react code into angular 6 Z4C4AD1FCA003CAAZDBB代码? - React.js to pain HTML: how to convert imageupload.html react code into angular 6 HTML code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM