简体   繁体   English

如何呈现在React.js中作为字符串属性传递的HTML标签

[英]How to render Html tags passed as string property in React.js

Is it possible to pass HTML tags as properties and then render in React.js 是否可以将HTML标签作为属性传递,然后在React.js中呈现

For example ES2015 syntax: 例如ES2015语法:

I add the property as a string with HTML tags: 我将属性添加为带有HTML标签的字符串:

renderItems(posts) {
    var groupDate = this.props.item.date.toUpperCase();
    var listCol = '<h4>' + groupDate.toString() + '</h4>';
    return (<ViewList list={posts} postCol1={listCol}/>);
}

And in my ViewList component I have: 在我的ViewList组件中,我有:

<div className="col-md-9">{this.props.postCol1}</div>

This is rendered as : 呈现为:

<h4>JANUARY 28TH, 2016</h4>

How do I get react to render the date excluding the HTML tags? 如何响应以渲染除HTML标记之外的日期?

Different views uses this component and tags may be different explaining why I would like to include the tag. 不同的视图使用此组件,标签可能会有所不同,这说明了为什么要包含标签。

Take the <h4> tags out of the listCol string. listCol字符串中取出<h4>标记。 Your ViewList component should render them: 您的ViewList组件应呈现它们:

<div className="col-md-9">
    <h4>
        {this.props.postCol1}
    </h4>
</div>

This actually worked: 这实际上有效:

renderItems(posts) {
     var groupDate = this.props.item.date.toUpperCase();
     return (<ViewList list={posts} postCol1={<h4>{groupDate.toString()}</h4>}/>);
   }

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

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