Giter Site home page Giter Site logo

inginx / react-native-webview-invoke Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pinqy520/react-native-webview-invoke

0.0 2.0 0.0 195 KB

Invoke functions between React Native and WebView directly

License: MIT License

JavaScript 63.39% Python 5.81% Java 4.97% Objective-C 15.49% HTML 10.34%

react-native-webview-invoke's Introduction

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

Run Demo

$ git clone [email protected]:pinqy520/react-native-webview-invoke.git
$ cd react-native-webview-invoke/examples/InvokeTest
$ react-native run-ios

Or open the iOS Xcode project under examples/InvokeTest/ios folder, and run.

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() {
        return <Webview
            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} />

react-native-webview-invoke's People

Contributors

pinqy520 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.