$ npx react-native init MyReactNativeApp
$ cd MyReactNativeApp
$ npm install react-native-webview --save
$ cd ios
$ npx pod-install ios
创建src/mywebview.js文件,并添加以下代码:
import React from 'react'
import { WebView } from 'react-native-webview';
export default function index() {
return (
<WebView
source={{ uri: 'https://cn.bing.com' }}
style={{ marginTop: 20, }}
/>
)
}
修改 App.tsx 文件,添加 MyWebView 组件的使用:
import React from 'react';
import { SafeAreaView, View, StyleSheet } from 'react-native';
import MyWebView from './src/webview/mywebview'
function App(): React.JSX.Element {
return (
<SafeAreaView style={styles.container}>
<MyWebView />
</SafeAreaView>
)
}
var styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#ffffff',
}
})
export default App
$ npx react-native start