Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/lesnitsky/react-native-webview-messaging

✉️ Send/Receive data between React Native app and WebView
https://github.com/lesnitsky/react-native-webview-messaging

communication event messaging-api react-native webview

Last synced: 5 days ago
JSON representation

✉️ Send/Receive data between React Native app and WebView

Awesome Lists containing this project

README

        

# React Native WebView Messaging

React Native WebView extension with 2-way event-based communication API

[![GitHub stars](https://img.shields.io/github/stars/lesnitsky/react-native-webview-messaging.svg?style=social)](https://github.com/lesnitsky/react-native-webview-messaging)
[![Twitter Follow](https://img.shields.io/twitter/follow/lesnitsky_dev.svg?label=Follow%20me&style=social)](https://twitter.com/lesnitsky_dev)

:warning: this is v2 branch which is not yet stable. Check out [v1 branch](https://github.com/R1ZZU/react-native-webview-messaging/tree/v1) for v1 docs

## Installation

```sh
npm install react-native-webview-messaging@next
```

## Examples

[Examples](https://github.com/R1ZZU/react-native-webview-messaging/tree/master/examples)

## Usage

### React Native

```javascript
import React, { Component } from 'react';
import { View, TouchableHighlight } from 'react-native';
import { connectToRemote, WebView } from 'react-native-webview-messaging';

export class ExampleView extends Component {
render() {
return (

{ this.webview = webview; }}
source={ require('./some-page.html') }
/>

Send message to WebView


)
}

async componentDidMount() {
this.remote = await connectToRemote(this.webview);

this.remote.on('text', text => console.log(text));
this.remote.on('json', json => console.log(json));
this.remote.on('custom-event-from-webview', eventData => console.log(eventData));
}

sendMessageToWebView = () => {
this.remote.sendJSON({
payload: 'JSON from RN'
});

this.remote.send('plain text from RN');

this.remote.emit('custom-event-from-rn', { payload: 'Custom event from RN' });
}
}
```

### Web

```javascript
import { connectToRemote } from 'react-native-webview-messaging/web';

connectToRemote()
.then(remote => {
remote.on('text', text => console.log(text));
remote.on('json', json => console.log(json));
remote.on('custom-event-from-rn', data => console.log(data));

remote.send('plain text from WebView');
remote.sendJSON({
payload: 'JSON from WebView'
});

remote.emit('custom-event-from-webview', { payload: 'Custom event from WebView' });
});
```

## LICENSE

MIT

[![GitHub stars](https://img.shields.io/github/stars/lesnitsky/react-native-webview-messaging.svg?style=social)](https://github.com/lesnitsky/react-native-webview-messaging)
[![Twitter Follow](https://img.shields.io/twitter/follow/lesnitsky_dev.svg?label=Follow%20me&style=social)](https://twitter.com/lesnitsky_dev)