如何在React Native中删除来自responseJson的html标签?

时间:2019-01-07 21:26:54

标签: react-native fetch

如何删除html标签?我需要显示说明,但React native不支持html标记。

json:

[
  {
    "id": 73,
    "name": "Hello World",
    "status": "publish",
    "description": "<p>hello</p>\n",
    ...

代码:

componentDidMount(){
  return fetch('example.com/testttttttttttttttttttttttttttttttttt')
  .then((response)=>response.json()).
  then((responseJson)=>{
    let ds = new ListView.DataSource({rowHasChanged:(r1,r2)=>r1!=r2});
    this.setState({
      isLoading:false,
      dataSource:ds.cloneWithRows(responseJson)
    });
  }).catch((error)=>console.error(error));
}

和listView:

<ListView
        dataSource={this.state.dataSource}
          renderRow={ (rowData)=>
                <View style={{width:'100%',height:40}}>
                  <View style={{marginTop:5,marginBottom:5}}>
                    <Text>{rowData.name}</Text>
                    <Text>{rowData.description}</Text>
                  </View>
                </View>
          }
      /> 

1 个答案:

答案 0 :(得分:0)

剥离html标签不是一个小问题。问题Strip HTML from Text JavaScript中讨论了许多问题。

我认为,最好的方法是使用striptags之类的库。然后您的代码将如下所示:

<Text>{striptags(rowData.description)}</Text>

另一个更幼稚的实现可以使用String.prototype.replace

<Text>{rowData.description.replace(/<(?:.|\n)*?>/gm, '')}</Text>