npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tuya/react-native-webview-invoke

v1.0.2

Published

Invoke functions between React Native and WebView directly

Downloads

17

Readme

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} />