5

I built new React Native project yesterday using react-native-cli. But when running the project with my android phone, I got this error in red screen.

Invariant Violation: WebView has been removed from React Native. It can now be installed and imported from 'react-native-webview' instead of 'react-native'. See 'https://github.com/react-native-community/react-native-webview'.

I never used WebView and here is my "package.json".

{
  "name": "",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint ."
  },
  "dependencies": {
    "react": "16.9.0",
    "react-native": "0.61.5",
    "react-native-image-slider": "^2.0.3",
    "react-native-svg": "^9.13.6",
    "react-navigation": "^4.0.10",
    "react-navigation-drawer": "^2.3.3",
    "react-navigation-stack": "^1.10.3",
    "react-redux": "^7.1.3",
    "redux": "^4.0.4"
  },
  "devDependencies": {
    "@babel/core": "^7.7.5",
    "@babel/runtime": "^7.7.6",
    "@react-native-community/eslint-config": "^0.0.5",
    "babel-jest": "^24.9.0",
    "eslint": "^6.7.2",
    "install-peers": "^1.0.3",
    "jest": "^24.9.0",
    "metro-react-native-babel-preset": "^0.57.0",
    "react-native-gesture-handler": "^1.5.2",
    "react-native-reanimated": "^1.4.0",
    "react-test-renderer": "16.9.0"
  },
  "jest": {
    "preset": "react-native"
  }
}

I tried to fix this problem for a whole day but I couldn't. I just think 'react-native-gesture-handler' causes this problem. Anyone who has much experience of React-Native, please help me. Thank you.

Andrew Terex
  • 341
  • 2
  • 4
  • 10
  • However, used yarn instead of npm. Removed node_modules folder and ran "yarn install" command, then it's okay. I'm not sure about the reason. Thank you for help, everyone. – Andrew Terex Dec 13 '19 at 01:23

4 Answers4

5

Run this command in your root folder: npm i react-native-webview


Please see this npm package first

https://www.npmjs.com/package/react-native-webview


Run the project again in your android and IOS device with this command: react-native run-android or react-native run-ios


Import the package:

import { WebView } from 'react-native-webview'

Use webview as:

<WebView source={{ uri: 'https://facebook.github.io/react-native/' }} />
Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42
sagar dhiman
  • 293
  • 2
  • 11
1

Basically your guess is correct, it's affected by react-native-gesture-handler.

Reason

This is the file that depends on webview.
https://github.com/software-mansion/react-native-gesture-handler/blob/1.0.10/GestureHandler.js

Solution

  1. upgrade the react-native-gesture-handler to latest version which has already replaced webview from react-native to react-native-webview
  2. run yarn install and rebuild your project
  • after update react-native-gesture-handler i got this error Execution failed for task ':react-native-gesture-handler:compileDebugJavaWithJavac – Ibad Ur Rehman Feb 06 '22 at 17:27
0

Try to

  1. delete all your build-files in android folder and ios folder
  2. delete your node_modules
  3. npm install
  4. Try to run it again
user1872384
  • 6,886
  • 11
  • 61
  • 103
-1

I think it's clearly mentioned there in the warning:

WebView has been removed from React Native. It can now be installed and imported from 'react-native-webview' instead of 'react-native'

I suppose you are now using the following some where in your code

import {
  WebView,
} from 'react-native';

Now onwards you shall do the following instead import { WebView } from 'react-native-webview';

To get started with react-native-webivew, you can follow the getting started document here

Tommy Leong
  • 2,509
  • 6
  • 30
  • 54