1.下载WebView
npm i react-native-webview
2.引入WebView
import React, { Component } from 'react';
import { WebView } from 'react-native'; //引入WebView
class MyInlineWeb extends Component {
render() {
return (
<WebView
originWhitelist={['*']}
source={{ html: '<h1>Hello world</h1>' }}
/>
);
}
}
一般会这样报一些错
Invariant Violation: requireNativeComponent: "RNCWKWebView" was not found in the UIManager.
Invariant Violation: requireNativeComponent: "RNCSlider" was not found in the UIManager.
null is not an object (evaluating '_RNGestureHandlerModule.default.Direction')
3.解决报错
react-native 0.60以上版本安装第三方库的时候会autolink 出现这个问题是 我安装 react-native-webview 之后运行 ios出现的,这是因为ios 没有自动安装依赖库
项目目录下
cd ios //进入ios目录
pod install //拉去项目的pod依赖
回到项目目录运行
react-native link react-native-webview
或者
yarn react-native link react-native-webview
然后重启项目就会发现没有报错可以正常使用了