这似乎很容易,但是由于某种原因我无法做到。
在React中我有一个对象:
const test = {
paragraphs: [
'text of paragraph A text <a>link</a> text', //<a> tag will be printed just as a string
'text of paragraph B text text',
]
}
我正在映射段落,以便呈现一些段落,就像这样。
test.paragraphs.map(item => <p>item</p>)
一切正常。但是,如果我想在第一段中放入链接标记,该怎么办。如何将html标记嵌入数组中的第一个字符串内?
最佳答案
这是一个如何在reactjs应用程序中将字符串转换为HTML的示例
class App extends React.Component {
constructor() {
super();
this.state = {
description: '<a href={to} target="_blank" {...rest}>{children}</a>'
}
}
render() {
return (
<div dangerouslySetInnerHTML={{ __html: this.state.description }} />
);
}
}
ReactDOM.render(<App />, document.getElementById('root'));
关于javascript - 将html标记放置在字符串中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54534895/