简体   繁体   English

DangerouslySetInnerHtml()显示React中上一页的内容

[英]DangerouslySetInnerHtml() showing content from previous page in React

Building a site which shows users profiles. 建立一个显示用户个人资料的网站。 When changing from one profile to another, the profile description is getting stuck with the previous users profile data. 当从一个配置文件更改为另一个配置文件时,配置文件描述将卡在先前的用户配置文件数据中。

For example 例如

  1. Visit users profile 1 访问用户个人资料1
  2. Users profile description reads User profile 1 description 用户配置文件描述读取User profile 1 description
  3. Visit users profile 2 访问用户个人资料2
  4. Users profile description reads User profile 1 description User profile 2 description 用户配置文件描述读取User profile 1 description User profile 2 description

My code is all being run in the render function of my component so should be rerendering when state updates. 我的代码全部在组件的render函数中运行,因此在状态更新时应重新渲染。

render(){
    return(
        <h4>Profile 1<h4>
        <p dangerouslySetInnerHTML={{__html: this.props.profile.description}} />
    )
}

Turns out this.props.profile.description was HTML wrapped in <p> tags. 原来this.props.profile.description是HTML包装在<p>标记中的。 This was causing p tags to be nested within p tags which causes issues as described in this post - Nesting <p> won't work while nesting <div> will? 这导致p标签嵌套在p标签内,这引起了本文中所述的问题- 嵌套<p>无法工作,而嵌套<div>可以吗?

Rule of thumb is never use dangerouslySetInnerHTML on a <p> tag. 经验法则永远不要在<p>标记上危险地使用SetInnerHTML。

Took me a long time to get to the root cause of this and thought it was worth passing it on. 花了我很长时间才找到这个问题的根本原因,并认为值得将其继续下去。

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

相关问题 危险地反应SetInnerHTML导致页面滚动到顶部 - React dangerouslySetInnerHTML causes page to scroll to top 限制 html 内容显示在 react 被危险的 setinnerhtml 显示 - Limit the html content to be displayed in react being displayed by dangerouslysetinnerhtml 当在React中使用危险地使用SetInnerHTML时,阻止HTML在CSS之前加载 - Prevent html from loading before css when using dangerouslySetInnerHTML in React 在页面上显示以前的值 - Showing Previous Values on page 阅读更多 + React 中的省略号 (...) for dangerouslySetInnerHTML - Read more + ellipsis (...) in React for dangerouslySetInnerHTML 使用危险地设置组件反应组件,包含CSS - React component with dangerouslySetInnerHTML, contain CSS 模态内容“页面”并从按钮返回上一页 - Modal content "pages" and going back to previous page from button HTML页面视图页面源未显示从angularjs加载的内容 - HTML Page view page source not showing content load from angularjs 在后台从 React 组件制作 HTML 字符串,如何在另一个 React 组件中通过危险的SetInnerHTML 使用该字符串 - Making an HTML string from a React component in background, how to use the string by dangerouslySetInnerHTML in another React component 在 div 中添加一个 span 并使用 dangerouslySetInnerHTML 来显示工具提示 - Add a span inside a div with dangerouslySetInnerHTML for showing a tooltip
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM