javascript - String to React DOM object - Stack Overflow

I'm using react js 15.4.2. I have html data stored as string like "<p>Hello..<p>

I'm using react js 15.4.2. I have html data stored as string like "<p>Hello..</p>". I can not append them to the DOM.

...
ponentDidMount(){
  let obj = dataFromDB.map((data)=>{
               return(
                  <div>
                    {data.strHtml}
                  </div>
               ) 
            }) 
   this.setState({storedObj: obj})
}

render(){
    return(
      <div>
          {this.state.storedObj}
      </div>
    )
}
...

Do you have any suggestions to append the html objects to the DOM properly ?

I'm using react js 15.4.2. I have html data stored as string like "<p>Hello..</p>". I can not append them to the DOM.

...
ponentDidMount(){
  let obj = dataFromDB.map((data)=>{
               return(
                  <div>
                    {data.strHtml}
                  </div>
               ) 
            }) 
   this.setState({storedObj: obj})
}

render(){
    return(
      <div>
          {this.state.storedObj}
      </div>
    )
}
...

Do you have any suggestions to append the html objects to the DOM properly ?

Share Improve this question asked Mar 2, 2018 at 14:23 kbrkkbrk 6601 gold badge10 silver badges27 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

you could also look into react-html-parser module for parsing html before rendering to the DOM it's pretty easy to use.

import Parser from 'html-react-parser';    

class Example extends React.Component {

render() {
   return (
     <div>
       {Parser(this.state.dataStored)}
     </div>
   )
}
}

You should use this code -

  return <div dangerouslySetInnerHTML={{__html: data.strHtml}} />;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745247442a4618475.html

相关推荐

  • javascript - String to React DOM object - Stack Overflow

    I'm using react js 15.4.2. I have html data stored as string like "<p>Hello..<p>

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信