React渲染动态HTML——dangerouslySetInnerHTML

来源:互联网 发布:手机淘宝怎样申请换货 编辑:程序博客网 时间:2024/05/20 23:04

使用场景,后台返回数据需要进行标签替换或者其它处理,生成新的标签属性,无法将 RestAPI 数据进行展示:

import React, {Component, PropTypes} from 'react';import * as formatContent from 'util/formatWbsContent.js';const getWbContent = (content) => {  return <div dangerouslySetInnerHTML={formatContent.createMarkup(content)}/>;}class ShowContent extends Component {    constructor(props){        super()    }        render(){     return(        {getWbContent(this.props.content)}     )    }    }
//'util/formatWbsContent.js'export function createMarkup(text) {  return {__html: getWbContent(text)};}export const getWbContent = (content) => {    /*    *    *一些标签替换等操作    *    */        return contentHTML;}
原创粉丝点击