在React Native中转换html字符串的正确方法



在React Native中,转换和显示没有html引号和标记的html字符串的正确方法是什么?

这是一个示例文本:

"What is the name of James Dean's character in the 1955 movie "Rebel Without a Cause"?"

在React中,dangerouslysetinnerhtml选项起到了作用,但在本机中我无法正确显示它。

React Native文档推荐React NativeWebView。

您也可以使用其他库,例如react原生htmlview。此库接受HTML内容并将其呈现为本机视图。

npm install react-native-render-html
import { ScrollView, useWindowDimensions } from "react-native";
import RenderHTML from "react-native-render-html";

function App() {
const { width } = useWindowDimensions();
const HTML = `What is the name of James Dean's character in the 1955 movie "Rebel Without a Cause"?`
return (
<ScrollView style={{ flex: 1 }}>
<RenderHTML contentWidth={width} source={{ HTML }} />
</ScrollView>
);
}

最新更新