2018-09-20 02:27:09 +00:00
# React Native WebView Getting Started Guide
Here's how to get started quickly with the React Native WebView.
2020-02-12 23:39:11 +00:00
## 1. Add react-native-webview to your dependencies
2018-09-20 02:27:09 +00:00
```
2019-02-28 23:26:28 +00:00
$ yarn add react-native-webview
2019-06-02 04:06:31 +00:00
```
2020-02-26 06:54:06 +00:00
(or)
For npm use
2019-06-02 04:06:31 +00:00
```
$ npm install --save react-native-webview
2018-09-20 02:27:09 +00:00
```
2020-02-12 23:39:11 +00:00
## 2. Link native dependencies
2018-09-20 02:27:09 +00:00
2019-08-02 08:45:00 +00:00
From react-native 0.60 autolinking will take care of the link step but don't forget to run `pod install`
2018-09-20 02:27:09 +00:00
React Native modules that include native Objective-C, Swift, Java, or Kotlin code have to be "linked" so that the compiler knows to include them in the app.
```
$ react-native link react-native-webview
```
2019-05-19 22:36:51 +00:00
2020-02-12 23:39:11 +00:00
_NOTE: If you ever need to uninstall React Native WebView, run `react-native unlink react-native-webview` to unlink it._
2020-04-24 17:54:17 +00:00
### iOS & macOS:
2019-07-02 15:45:34 +00:00
2020-04-24 17:54:17 +00:00
If using CocoaPods, in the `ios/` or `macos/` directory run:
2020-02-26 06:54:06 +00:00
2019-07-02 15:45:34 +00:00
```
$ pod install
```
2019-05-19 22:36:51 +00:00
2020-04-24 17:54:17 +00:00
While you can manually link the old way using [react-native own tutorial ](https://reactnative.dev/docs/linking-libraries-ios ), we find it easier to use CocoaPods.
If you wish to use CocoaPods and haven't set it up yet, please instead refer to [that article ](https://engineering.brigad.co/demystifying-react-native-modules-linking-ae6c017a6b4a ).
2020-02-12 23:39:11 +00:00
### Android:
2019-05-20 08:25:52 +00:00
Android - react-native-webview version < 6:
This module does not require any extra step after running the link command 🎉
Android - react-native-webview version >=6.X.X:
2019-05-19 22:36:51 +00:00
Please make sure AndroidX is enabled in your project by editting `android/gradle.properties` and adding 2 lines:
```
android.useAndroidX=true
android.enableJetifier=true
```
2019-01-31 15:32:14 +00:00
For Android manual installation, please refer to [this article ](https://engineering.brigad.co/demystifying-react-native-modules-linking-964399ec731b ) where you can find detailed step on how to link any react-native project.
2020-03-17 02:30:25 +00:00
### Windows:
Autolinking is not yet supported for ReactNativeWindows. Make following additions to the given files manually:
#### **windows/myapp.sln**
Add the `ReactNativeWebView` project to your solution.
1. Open the solution in Visual Studio 2019
2. Right-click Solution icon in Solution Explorer > Add > Existing Project
Select `node_modules\react-native-webview\windows\ReactNativeWebView\ReactNativeWebView.vcxproj`
#### **windows/myapp/myapp.vcxproj**
Add a reference to `ReactNativeWebView` to your main application project. From Visual Studio 2019:
1. Right-click main application project > Add > Reference...
2020-04-24 17:54:17 +00:00
Check `ReactNativeWebView` from Solution Projects.
2020-03-17 02:30:25 +00:00
2. Modify files below to add the package providers to your main application project
#### **pch.h**
Add `#include "winrt/ReactNativeWebView.h"` .
#### **app.cpp**
Add `PackageProviders().Append(winrt::ReactNativeWebView::ReactPackageProvider());` before `InitializeComponent();` .
2020-02-12 23:39:11 +00:00
## 3. Import the webview into your component
2018-09-20 02:27:09 +00:00
```js
import React, { Component } from 'react';
import { WebView } from 'react-native-webview';
class MyWeb extends Component {
render() {
return (
< WebView
2019-05-19 22:36:51 +00:00
source={{ uri: 'https://infinite.red' }}
style={{ marginTop: 20 }}
2018-09-20 02:27:09 +00:00
/>
);
}
}
```
Minimal example with inline HTML:
```js
import React, { Component } from 'react';
import { WebView } from 'react-native-webview';
class MyInlineWeb extends Component {
render() {
return (
< WebView
originWhitelist={['*']}
source={{ html: '< h1 > Hello world< / h1 > ' }}
/>
);
}
}
```
Next, check out the [API Reference ](Reference.md ) or [In-Depth Guide ](Guide.md ).