React Native 截取后端数据返回Html标签中的值

来源:互联网 发布:matlab读取mat数据 编辑:程序博客网 时间:2024/06/05 09:59
import React, { Component } from 'react';import {    StyleSheet,    View,    Dimensions,    Text} from 'react-native';import { Widget } from 'rn-yunxi';const { width, height } = Dimensions.get('window');//模块声名并导出export default class EmText extends Component {    //默认属性    static defaultProps = {        str: '<em>红酒</em>',    };    //构造函数    constructor(props) {        super(props);        let normalText = [];        let specialText = [];        if (props.str && props.str !== '' && props.str.length > 0) {            try {                let tempText = props.str;                tempText = tempText.replace(/<[^>]*>/g, 'tmd')                tempText.trim().split('tmd').forEach((obj, index) => {                    if ((index + 1) % 2 === 0) {                        normalText.push(obj)                    } else {                        specialText.push(obj)                    }                })            } catch (e) { }        }        this.state = { //状态机变量声明            normalText,            specialText,        }    }    //渲染    render() {        const { normalText,            specialText, } = this.state        if (normalText && normalText.length > 0) {            return (                <Text >                    {normalText.map((obj, i) => {                        return (<Text>{obj}                            <Text style={{ color: 'red' }}>{specialText[i]}</Text>                        </Text>)                    })}                </Text>            );        }        return (            <Text >{this.props.str}</Text>        );    }};

使用方法:

<EmText str={'<a>获取后端数据返回Html标签中的值</a>'} />

取到的值就是: 获取后端数据返回Html标签中的值

原创粉丝点击