如何在移动端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
相关推荐
小妖怪的夏天1 天前
react native android设置邮箱,进行邮件发送
android·spring boot·react native
千码君20161 天前
React Native:关于react自定义css属性的位置
css·react native·react.js·前端框架·ecmascript·组件嵌套
KenXu2 天前
React Conf 2025: React Native 新架构与生态详解
react native
千码君20163 天前
React Native:快速熟悉react 语法和企业级开发
javascript·react native·react.js·vite·hook
ps_xiaowang6 天前
React Query入门指南:简化React应用中的数据获取
前端·其他·react native·react.js
chenbin___8 天前
鸿蒙键盘遮挡react native内容尝试
react native·harmonyos
Winson℡9 天前
React Native 中的 useCallback
javascript·react native·react.js
Winson℡12 天前
如何理解 React Native 中的 useEffect
javascript·react native·react.js
wayne21412 天前
React Native启动性能优化实战:Hermes + RAM Bundles + 懒加载
react native·react.js·性能优化