• Stars
    star
    232
  • Rank 172,847 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 8 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Invoke functions between React Native and WebView

react-native-webview-invoke

中文文档

npm version

Invoke functions between React Native and WebView directly

react-native-webview-bridge Support

Just like:

// Side A
const answer = await ask(question) 

// Side B
function ask(question) { return 'I don\'t know' }

Before using like that, you should firstly define the function you want to expose.

// Side A
const ask = invoke.bind('ask')

// Side B
invoke.define('ask', ask)

rnwm

Installation

$ npm install react-native-webview-invoke --save

Requires:

  • React Native >= 0.37

Basic Usage

There are two sides: native and web.

React Native Side

Import

import createInvoke from 'react-native-webview-invoke/native'

Create invoke

class SomePage extends React.Component {
    webview: WebView
    invoke = createInvoke(() => this.webview)
    render() {
        // Note: add 'useWebKit' property for rn > 0.59
        return <Webview useWebKit
            ref={webview => this.webview = webview}
            onMessage={this.invoke.listener}
            source={require('./index.html')}
            />
    }
}

Now, we can start to expose functions for Web, and get the function from Web. (See Start to use)

Web Side

Import

import invoke from 'react-native-webview-invoke/browser'

Or use <script> in .html

<script src="./node_modules/react-native-webview-invoke/dist/browser.umd.js"></script>
<script>
var invoke = window.WebViewInvoke
</script>

Start to use

For better illumination, we define two sides named A and B. each of them can be React Native or Web, and if A is React Native, then B is Web.

Assume that there are some functions in A side.

function whatIsTheNameOfA() {
    return 'A'
}

function tellAYouArea(someone: string, prefix: string) {
    return 'Hi, ' + prefix + someone + '!'
}

Expose them for B side.

invoke
    .define('whatIsTheNameOfA', whatIsTheNameOfA)
    .define('tellAYouArea', tellAYouArea)

OK, Go to the B side:

Firstly, bind some functions which exposed from A.

const whatIsTheNameOfA = invoke.bind('whatIsTheNameOfA')
const tellAYouArea = invoke.bind('tellAYouArea')

Now, we can use them.

await whatIsTheNameOfA()
// 'A'
await tellAYouArea('B', 'Mr.')
// 'Hi, Mr.B'

In addition, you can use them without definition too.

await invoke.fn.whatIsTheNameOfA()
// 'A'
await invoke.fn.tellAYouArea('B', 'Mr.')
// 'Hi, Mr.B'

API

createInvoke(getWebViewInstance)

(RN only) create invoke with the Webview instance.

Args:

  • getWebViewInstance [() => React.WebView] getter for Webview instance

Return:

  • invoke [invoke] invoke object

invoke.define(name, fn)

expose function to another side.

Args:

  • name [string] function name
  • fn [Function]

invoke.bind(name)

get function from another side

Args:

  • name [string] function name

Return:

[(...args: any[]) => Promise<any>] function

invoke.fn

All functions that defined at the other side

Usage

// A side
invoke.define('test', test)

// B side
invoke.fn.test()

invoke.listener

(RN only) the handler for the onMessage property of WebView element.

Usage:

<WebView onMessage={invoke.listener} />

More Repositories

1

mobx-persist

persist mobx stores
TypeScript
561
star
2

revas

Use React and CSS to build UI interfaces on canvas
TypeScript
114
star
3

three-typescript-starter

Three.js + Typescript + Webpack Boilerplate
TypeScript
109
star
4

react-native-shadow-view

React Native's View Component with Shadows Both on Android and iOS, inspired by react-native-shadow
TypeScript
35
star
5

yoga-layout-wasm

yoga-layout webassembly module
C++
28
star
6

GLMultiCameraPreviewDemo

Android multiple camera preview surface with offscreen rendering and GPU filter powered by android-gpuimage-plus. 安卓相机多预览,外加离屏渲染和GPU滤镜 (来自 android-gpuimage-plus) 示例
Java
14
star
7

coverage-shaker

Minify JavaScript code from chrome/puppeteer coverage report. tree shaking.
TypeScript
11
star
8

react-native-emoji-rain

React native emoji rain component use react hooks - inspired by WeChat emoji rain
JavaScript
10
star
9

flutter_tencentmusician

An unofficial app for tencent musician using Flutter | 用Flutter实现的腾讯音乐人后台
Dart
5
star
10

mil.

TypeScript版冲顶大会、百万xx等辅助答题程序(还在开发中,预计今晚搞定吧)
TypeScript
3
star
11

apollo-client-tsx-starter-kit

React Webpack TypeScript Apollo Client Side Boilerplate For GraphQL API
JavaScript
2
star
12

five

An example app with firebase JSSDK + React Native + Redux
JavaScript
2
star
13

simple-tsx-starter-kit

Minimal React Webpack TypeScript Boilerplate
JavaScript
1
star
14

SShareNS3

Simulation of an ad-hoc Semantic Web Data System based on NS3
C++
1
star
15

react-native-webviewbridge-invoke

react-native-webview-invoke for react-native-webview-bridge
JavaScript
1
star