首先 webview已經從 react-native 移到單獨模塊 react-native-webview;
使用:
- 安裝 yarn add react-native-webview;
- 運行 react-native link react-native-webview(native0.6以後會自動link 不知道可不可以 )
如果報錯:
> Invariant Violation: requireNativeComponent: "RNCWKWebView" was not found in the UIManager. >
- cd ios
- 運行 pod install
- 重新運行 react-native run-ios
在App.js代碼裏寫入
import React, { Component } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { WebView } from 'react-native-webview';
// ...
export default class MyWebComponent extends Component {
render() {
return <WebView source={{ uri: 'https://www.baidu.com' }} />;
}
}