最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - I want to render dynamic content from back-end that has html tags in react native - Stack Overflow

programmeradmin0浏览0评论

I have created an app that uses woo-merce as back-end, the problem is many of the attributes of products that I receive from back-end are in html, when I tried to render it in it treated everything as text and as a result the whole string got printed to the screen with all the html tags which is not the desired result .

Is there any trick except for the Web-View to solve this problem?

I have created an app that uses woo-merce as back-end, the problem is many of the attributes of products that I receive from back-end are in html, when I tried to render it in it treated everything as text and as a result the whole string got printed to the screen with all the html tags which is not the desired result .

Is there any trick except for the Web-View to solve this problem?

Share Improve this question edited Sep 24, 2018 at 20:53 Maria 4,6211 gold badge27 silver badges27 bronze badges asked Sep 24, 2018 at 17:56 Ammar TariqAmmar Tariq 8472 gold badges14 silver badges30 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

For me the best way to render HTML code is to use a library called react-native-htmlview.

This is a simple example:

import React, {Component} from 'react';
import {
  View,
  StyleSheet,
} from 'react-native';
import HTMLView from 'react-native-htmlview';

class App extends Component {
  state = {
    html: '<p>Some Dummy <b>HTML</b> code</p>'
  }

  render() {
    return (
        <View style={styles.container}>
          <HTMLView
              value={this.state.html}
              stylesheet={htmlStyleSheet}
          />
        </View>
    );
  }
}

const htmlStyleSheet = StyleSheet.create({
  p: {
    color: 'red'
  },
  b: {
    color: 'black'
  }
})

const styles = StyleSheet.create({
  container: {
    paddingTop: 20,
  }
})

export default App;

For more information:

https://github./archriss/react-native-render-html

Another option is to use WebView with source prop:

https://facebook.github.io/react-native/docs/webview

import React, { Component } from 'react';
import { WebView } from 'react-native';

class MyInlineWeb extends Component {
  render() {
    return (
      <WebView
        originWhitelist={['*']}
        source={{ html: '<h1>Hello world</h1>' }}
      />
    );
  }
}
发布评论

评论列表(0)

  1. 暂无评论