简体   繁体   English

如何使用危险的SetInnerHTML加载内容来处理“多读或少读”

[英]How to handle "read more or less" using dangerouslySetInnerHTML to load Content

I need to apply "read more or less" in react component.我需要在反应组件中应用“多读或少读”。 The problem is that content in response is in html from or you can say rich text with all the问题是响应的内容是来自 html 的,或者你可以说所有的富文本

tags in it.其中的标签。 And from frontend i am handling this using从前端我正在使用

<div
            style={{ padding: 15 }}
            className="text-right single-content"
            dangerouslySetInnerHTML={{ __html: post.content }}
/>

But now as i am using react library 'react-read-more-less' i have to write the component and have to load the content in this way但是现在当我使用反应库“react-read-more-less”时,我必须编写组件并以这种方式加载内容

<ReadMoreAndLess
                  ref={this.ReadMore}
                  className="read-more-content"
                  charLimit={250}
                  readMoreText="Read more"
                  readLessText="Read less"
>
                  {post.content}
  </ReadMoreAndLess>

But its not working properly its loading each tags associated with the content.但是它无法正常工作,它加载与内容相关的每个标签。 enter image description here在此处输入图片说明

Thanks .谢谢 。

如果您的帖子内容是 HTML,则需要使用类似https://www.npmjs.com/package/react-html-parser 的内容来避免使用dangerouslySetInnerHTML

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM