0.7.6 • Published 2 years ago

react-native-webview-comlink v0.7.6

Weekly downloads
15
License
MIT
Repository
github
Last release
2 years ago

react-native-webview-comlink Node.js CI

react-native-webview-comlink brings addJavascriptInterface to react-native-webview, supports both Android and iOS. Its implemention is inspired by the Comlink project.

Install

  • Install the package: npm i --save react-native-webview-comlink

Usage

Native

import { WebView } from 'react-native-webview';
import { withJavascriptInterface, Remote } from 'react-native-webview-comlink';
// it's not necessary but recommended to put the interface definition at some common
// place to share it between native and web projects if you use TypeScript
import { MyJSInterface } from './common/types';

// root api object for web side to call
const rootObj: MyJSInterface = {
    someMethod() {
        console.warn('someMethod called');
        return 42;
    },
    someMethodWithCallbackSupport(cb: Remote<(msg: string) => void>) {
        cb('invoke callback from native');

        // release the callback, so it can be garbage collected at the web side.
        // callbacks maintain reference count inside, each time we got a callback instance
        // from the method argument, there should be a corresponding release() call when it
        // is no longer needed.
        // this can be safely skipped if FinalizationRegistry and WeakRef is supported at
        // native Javascript runtime.
        // however, since GC timing is unpredictable, it's still recommended to handle the
        // release() manually to get a lower memory footprint at the web side. (and avoids
        // possible lagging if lots of proxied method being cleaned up during GC - which causes
        // the same amount of messages being sent to web side)
        cb.release();
    },
};
const WebViewComponent = withJavascriptInterface(rootObj, 'MyJSInterface')(WebView);

// render web page with the <WebViewComponent />

Web

import { JavascriptInterface } from 'react-native-webview-comlink';
import { MyJSInterface } from './common/types';

declare global {
    interface Window {
        MyJSInterface: JavascriptInterface<MyJSInterface>;
    }
}

// call native side method
MyJSInterface.someMethod().then((result) => {
    console.log(result);
});

// callbacks are supported
MyJSInterface.someMethodWithCallbackSupport((msg) => {
    console.log(msg);
});

Examples

There are example React Native project and Web project(React) in examples directory

API

Native

withJavascriptInterface(obj, name, options)(WebView)

Returns a higher-order React WebView component class that has obj exposed as name.

  • options (Object): if specified, customized the behavior of the higher-order WebView component.
    • whitelistURLs (String or RegExp Array): white list urls where the JavascriptInterface enabled, default is null
    • isEnabled (Function): for gain more control on enable or disable status besides whitelistURLs, it gets called in sending and receiving each message, returns true to let the message pass, default is null
    • forwardRef (Boolean): forward ref to the wrapped WebView component, default is false
    • log (Boolean): print debug log to console, default is false
// you may add multiple javascript interface by nest the `withJavascriptInterface` call, e.g. following code adds both `MyAPI` and `MyAPI2` to web page
withJavascriptInterface(apiObj2, 'MyAPI2')(withJavascriptInterface(apiObj, 'MyAPI')(WebView))

Web

Just call JavascriptInterface methods on window.name.

Compatibility

  • Android 5+
  • iOS 10+
0.7.6

2 years ago

0.7.5

3 years ago

0.7.4

3 years ago

0.7.3

3 years ago

0.7.2

3 years ago

0.7.1

3 years ago

0.7.0

3 years ago

0.6.4

3 years ago

0.6.3

3 years ago

0.6.2

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.4.4

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.5

5 years ago

0.3.4

5 years ago

0.3.3

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago

0.0.1

5 years ago