如何在移动端app里嵌套web页面之react-native-webview

文章目录


前言

快速上手使用react-native-webview


官方指南,可查看详细使用教程

react-native-webview介绍

React Native WebView是 React Native 的社区维护的 WebView 组件。它旨在替代内置的 WebView(已从核心中移除)。

简单来说,就是可以实现将web页面嵌套在app里面,实现混合开发。

在expo中使用

安装

javascript 复制代码
  // 官方推荐

 npm install --save react-native-webview
 // 对于pnpm
 npm install --save react-native-webview

使用

javascript 复制代码
import React, { Component } from 'react';
import { WebView } from 'react-native-webview';

class MyWeb extends Component {
  render() {
    return (
      <WebView
        source={{ uri: 'https://infinite.red' }}
        style={{ marginTop: 20 }}
      />
    );
  }
}

问题

如果使用npm或pnpm安装,可能报以下错误

报错

javascript 复制代码
Invariant Violation: "main" has not been registered. This can happen if:
* Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project.
* A module failed to load due to an error and AppRegistry.registerComponent wasn't called., js engine: hermes

解决

版本不匹配问题,可以使用expo直接安装

javascript 复制代码
expo install react-native-webview
相关推荐
杉氧12 小时前
状态管理变迁史:为什么我们放弃了 Redux 选择 Zustand?
android·前端·react native
光影少年1 天前
react navite 安卓iOS 打包、签名、环境区分
前端·react native·react.js
墨狂之逸才1 天前
React Native 环境变量方案选型:自定义、react-native-dotenv 与 react-native-config
react native
杉氧1 天前
React 灵魂:深入剖析 Hooks 与副作用(Side Effects)陷阱
android·前端·react native
光影少年3 天前
react navite调试方案:Flipper、远程调试
前端·javascript·react native·react.js·前端框架
光影少年5 天前
react navite环境与工程化
前端·react native·react.js
真夜6 天前
RN热更新安全问题
前端·react native
杉氧6 天前
从 Modifier 到 Flexbox:React Native 布局与样式设计哲学
android·前端·react native