react-native-webview/docs/Getting-Started.md

97 lines
2.9 KiB
Markdown
Raw Normal View History

# 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
```
2019-02-28 23:26:28 +00:00
$ yarn add react-native-webview
```
(or)
For npm use
```
$ npm install --save react-native-webview
```
2020-02-12 23:39:11 +00:00
## 2. Link native dependencies
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`
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
```
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._
### iOS:
If using cocoapods in the `ios/` directory run
```
$ pod install
```
2020-02-12 23:39:11 +00:00
For iOS, while you can manually link the old way using [react-native own tutorial](https://facebook.github.io/react-native/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).
### 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:
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-02-12 23:39:11 +00:00
### macOS:
2020-02-12 23:39:11 +00:00
Cocoapod and autolinking is not yet support for react-native macOS but is coming soon. In the meantime you must manually link.
The method is nearly identical to the [manual linking method for iOS](https://facebook.github.io/react-native/docs/linking-libraries-ios#manual-linking) except that you will include the `node_modules/react-native-webview/macos/RNCWebView.xcodeproj` project in your main project and link the `RNCWebView-macOS.a` library.
2020-02-12 23:39:11 +00:00
## 3. Import the webview into your component
```js
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 }}
/>
);
}
}
```
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).