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

时间:2017-08-02 04:30:38

标签: html reactjs

构建显示用户个人资料的网站。从一个配置文件更改为另一个配置文件时,配置文件描述会卡在以前的用户配置文件数据中。

例如

  1. 访问用户个人资料1
  2. 用户个人资料说明为User profile 1 description
  3. 访问用户个人资料2
  4. 用户个人资料说明为User profile 1 description User profile 2 description
  5. 我的代码全部在我的组件的render函数中运行,所以应该在状态更新时重新渲染。

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

1 个答案:

答案 0 :(得分:0)

结果this.props.profile.description包含在<p>标记中的HTML。这导致p代码嵌套在p代码中,导致此帖中所述的问题 - Nesting <p> won't work while nesting <div> will?

经验法则永远不会在<p>标记上使用危险的SetInnerHTML。

花了很长时间才找到了根本原因,并认为值得传递它。

相关问题