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 badges2 Answers
Reset to default 6you 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
评论列表(0条)