Amm*_*riq 3 javascript cross-platform webview hybrid-mobile-app react-native
我创建了一个使用 woo-commerce 作为后端的应用程序,问题是我从后端收到的产品的许多属性都在 html 中,当我尝试在其中呈现它时,将所有内容都视为文本和文本结果整个字符串与所有 html 标签一起打印到屏幕上,这不是所需的结果。
除了Web-View还有什么技巧可以解决这个问题吗?
小智 5
对我来说,渲染 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;
Run Code Online (Sandbox Code Playgroud)
了解更多信息:
https://github.com/archriss/react-native-render-html
另一种选择是将 WebView 与 source 属性一起使用:
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>' }}
/>
);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5532 次 |
| 最近记录: |