[英]HTML tags and special characters received from backend of HackerNewsApi, not decoded in the view
I am retrieving JSON data from the hackerNews Api.我正在从hackerNews Api 检索JSON 数据。 I received the comments stored in the back-end as below:我收到存储在后端的评论如下:
by: "metakermit"
id: 19665566
kids: [19665689]
parent: 19664663
text: "A 15% cost reduction – not bad.<p>Possibly interesting – I'm working on developing a similar battery - solar panel off-the-shelf system that would be suited for people who live in cities (e.g. if you want to put a solar panel on your balcony):<p><a href="https://www.craftstrom.com/" rel="nofollow">https://www.craftstrom.com/</a>"
time: 1555338679
type: "comment" .
When I rendered the above data in the browser view, the special characters and html tags were shown as it is in the JSON.当我在浏览器视图中呈现上述数据时,特殊字符和 html 标签显示为 JSON 中的原样。
Can somebody help please?有人可以帮忙吗?
you can use the dangerouslySetInnerHTML
attribute/directive to tell react to interpret the html as it is.您可以使用dangerouslySetInnerHTML
属性/指令来告诉 react 按原样解释 html。
Example例子
class App extends Component {
constructor() {
super();
this.state = {
name: 'React'
};
}
createMarkup() {
return {__html: '<h3> this is a header </h3> normal text'};
}
render() {
return (
<div>
<div dangerouslySetInnerHTML={this.createMarkup()}></div>
</div>
);
}
}
Working stackblitz工作堆栈闪电战
Reference:参考:
I used html-react-parser library and it worked for me.我使用了 html-react-parser 库,它对我有用。 https://github.com/remarkablemark/html-react-parser#usage https://github.com/remarkablemark/html-react-parser#usage
Big Thanks @Abdelkarim EL AMEL.非常感谢@Abdelkarim EL AMEL。 I appreciate your reply.我很感激你的回复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.