【问题标题】:I want to render dynamic content from back-end that has html tags in react native我想从后端呈现动态内容,该后端在本机反应中具有 html 标签
【发布时间】:2018-09-24 17:56:09
【问题描述】:

我创建了一个使用 woo-commerce 作为后端的应用程序,问题是我从后端收到的许多产品属性都在 html 中,当我尝试在其中呈现它时,它会将所有内容都视为文本结果整个字符串被打印到屏幕上,所有的 html 标签都不是想要的结果。

除了 Web-View 有什么技巧可以解决这个问题吗?

【问题讨论】:

    标签: javascript react-native webview cross-platform hybrid-mobile-app


    【解决方案1】:

    对我来说,呈现 HTML 代码的最佳方式是使用名为 react-native-htmlview 的库。

    这是一个简单的例子:

    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;
    

    更多信息:

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

    另一种选择是使用带有源属性的 WebView:

    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>' }}
          />
        );
      }
    }
    

    【讨论】:

    • 感谢您的快速回复。有没有其他方式已经是内置功能并且不需要外部库?
    • 就像我说的你可以使用 WebView(这是我知道的唯一本机解决方案),我编辑了上面的答案 :)
    • 非常感谢@Naoufal
    • 我无法使用 HTMLView 在 android 设备中加载图像。在 ios 设备中,一切看起来都很完美。
    猜你喜欢
    • 2021-12-22
    • 1970-01-01
    • 2011-12-06
    • 2022-01-10
    • 1970-01-01
    • 2018-02-03
    • 2022-10-05
    • 1970-01-01
    • 2014-06-24
    相关资源
    最近更新 更多