{"id":19984184,"url":"https://github.com/pinqy520/react-native-webview-invoke","last_synced_at":"2025-04-04T22:02:39.263Z","repository":{"id":49379829,"uuid":"74781537","full_name":"pinqy520/react-native-webview-invoke","owner":"pinqy520","description":"Invoke functions between React Native and WebView","archived":false,"fork":false,"pushed_at":"2020-06-05T10:20:06.000Z","size":453,"stargazers_count":236,"open_issues_count":7,"forks_count":44,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-04-24T14:58:48.815Z","etag":null,"topics":["bridge","hybrid","invoke","jsbridge","native","react","react-native","webview"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pinqy520.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-11-25T18:53:52.000Z","updated_at":"2024-04-24T14:58:48.816Z","dependencies_parsed_at":"2022-08-26T05:20:30.373Z","dependency_job_id":null,"html_url":"https://github.com/pinqy520/react-native-webview-invoke","commit_stats":null,"previous_names":["pinqy520/react-native-webview-messager"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pinqy520%2Freact-native-webview-invoke","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pinqy520%2Freact-native-webview-invoke/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pinqy520%2Freact-native-webview-invoke/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pinqy520%2Freact-native-webview-invoke/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pinqy520","download_url":"https://codeload.github.com/pinqy520/react-native-webview-invoke/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247256103,"owners_count":20909240,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bridge","hybrid","invoke","jsbridge","native","react","react-native","webview"],"created_at":"2024-11-13T04:17:51.965Z","updated_at":"2025-04-04T22:02:39.241Z","avatar_url":"https://github.com/pinqy520.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# react-native-webview-invoke\n\n[中文文档](https://github.com/pinqy520/react-native-webview-invoke/wiki/%E4%B8%AD%E6%96%87%E6%96%87%E6%A1%A3)\n\n[![npm version](https://badge.fury.io/js/react-native-webview-invoke.svg)](https://badge.fury.io/js/react-native-webview-invoke)\n\n\u003e Invoke functions between React Native and WebView directly\n\n[`react-native-webview-bridge` Support](https://github.com/pinqy520/react-native-webviewbridge-invoke)\n\nJust like: \n\n``` javascript\n// Side A\nconst answer = await ask(question) \n\n// Side B\nfunction ask(question) { return 'I don\\'t know' }\n```\n\nBefore using like that, you should firstly define the function you want to expose.\n\n``` javascript\n// Side A\nconst ask = invoke.bind('ask')\n\n// Side B\ninvoke.define('ask', ask)\n```\n\n![rnwm](https://cloud.githubusercontent.com/assets/5719833/20641896/1fb6431c-b43d-11e6-83ec-3fe78e49220f.gif)\n\n## Installation\n\n```\n$ npm install react-native-webview-invoke --save\n```\n\nRequires：\n\n- React Native \u003e= 0.37\n\n## Basic Usage\n\nThere are two sides: native and web.\n\n### React Native Side\n\nImport\n\n``` javascript\nimport createInvoke from 'react-native-webview-invoke/native'\n```\n\nCreate `invoke`\n\n``` javascript\nclass SomePage extends React.Component {\n    webview: WebView\n    invoke = createInvoke(() =\u003e this.webview)\n    render() {\n        // Note: add 'useWebKit' property for rn \u003e 0.59\n        return \u003cWebview useWebKit\n            ref={webview =\u003e this.webview = webview}\n            onMessage={this.invoke.listener}\n            source={require('./index.html')}\n            /\u003e\n    }\n}\n```\n\nNow, we can start to expose functions for Web, and get the function from Web. (See Start to use)\n\n### Web Side\n\nImport\n\n``` javascript\nimport invoke from 'react-native-webview-invoke/browser'\n```\n\nOr use `\u003cscript\u003e` in `.html`\n\n``` html\n\u003cscript src=\"./node_modules/react-native-webview-invoke/dist/browser.umd.js\"\u003e\u003c/script\u003e\n\u003cscript\u003e\nvar invoke = window.WebViewInvoke\n\u003c/script\u003e\n```\n\n### Start to use\n\nFor 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.\n\nAssume that there are some functions in A side.\n\n``` javascript\nfunction whatIsTheNameOfA() {\n    return 'A'\n}\n\nfunction tellAYouArea(someone: string, prefix: string) {\n    return 'Hi, ' + prefix + someone + '!'\n}\n```\n\nExpose them for B side.\n\n``` javascript\ninvoke\n    .define('whatIsTheNameOfA', whatIsTheNameOfA)\n    .define('tellAYouArea', tellAYouArea)\n```\n\n---\n\nOK, Go to the B side:\n\nFirstly, bind some functions which exposed from `A`.\n\n``` javascript\nconst whatIsTheNameOfA = invoke.bind('whatIsTheNameOfA')\nconst tellAYouArea = invoke.bind('tellAYouArea')\n```\n\nNow, we can use them.\n\n``` javascript\nawait whatIsTheNameOfA()\n// 'A'\nawait tellAYouArea('B', 'Mr.')\n// 'Hi, Mr.B'\n```\n\nIn addition, you can use them without definition too.\n\n``` javascript\nawait invoke.fn.whatIsTheNameOfA()\n// 'A'\nawait invoke.fn.tellAYouArea('B', 'Mr.')\n// 'Hi, Mr.B'\n```\n\n## API\n\n### `createInvoke(getWebViewInstance)`\n\n\u003e (RN only) create `invoke` with the `Webview` instance.\n\nArgs:\n\n- getWebViewInstance [`() =\u003e React.WebView`] getter for `Webview` instance\n\nReturn:\n\n- invoke [`invoke`] invoke object\n\n### `invoke.define(name, fn)`\n\n\u003e expose function to another side.\n\nArgs:\n\n- name [`string`] function name\n- fn [`Function`] \n\n### `invoke.bind(name)`\n\n\u003e get function from another side\n\nArgs:\n\n- name [`string`] function name\n\nReturn:\n\n[`(...args: any[]) =\u003e Promise\u003cany\u003e`] function\n\n\n### `invoke.fn`\n\n\u003e All functions that defined at the other side\n\nUsage\n\n``` javascript\n// A side\ninvoke.define('test', test)\n\n// B side\ninvoke.fn.test()\n```\n\n\n### `invoke.listener`\n\n\u003e (RN only) the handler for the `onMessage` property of `WebView` element.\n\nUsage:\n\n``` javascript\n\u003cWebView onMessage={invoke.listener} /\u003e\n```\n\n\n\n\n ","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpinqy520%2Freact-native-webview-invoke","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpinqy520%2Freact-native-webview-invoke","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpinqy520%2Freact-native-webview-invoke/lists"}