Set of utils for communication between webview and react-native
npm install react-native-webview-utils
or
yarn add react-native-webview-utils
Instantiate a WebView, get a ref on it and pass it injectedJavascript
import React, { useRef } from 'react';
import { injectedJavascript, generateOnMessageFunction } from "react-native-webview-utils";
const Component = () => {
const ref = useRef();
return (
<WebView
ref={ref}
injectedJavascript={injectedJavascript}
/>
);
}
Use the ref to send messages
ref.current?.injectJavascript(generateOnMessageFunction(data));
Initialize the bridge a WebView, and send messages !
import React, { useEffect } from 'react';
import { initializeWebViewBridge, sendMessageToReactNative } from "react-native-webview-utils";
const Component = () => {
useEffect(() => {
initializeWebViewBridge((data) => {
// Use data received from react native
console.log(data);
});
sendMessageToReactNative({Hello: "from web"});
}, [])
}
MIT