I am working on a project where I need to send data (specifically gyroscope data) from a React Native app to a React app rendered inside a WebView. However, I'm facing difficulties in successfully transmitting this data. For simplicity reason I want to start by just sending a string and be able to access it in my react app.
What I Have Tried:
I have a React Native app that is supposed to send data to the React app. The React app is loaded inside a WebView in the React Native app. I attempted to use postMessage from React Native and listen for the message in the React app. Yet for some reason I am not able to find and use this data in my React App. he message/data doesn't seem to be received by the React app. I am not able to see any logs or data updates on the React app side.
Am I missing something in how I'm sending the data from React Native to the WebView? Is there a specific step or configuration required to enable communication between React Native and the WebView? Are there any known issues or limitations with using postMessage for this purpose?
React Native Code:
const webViewRef = React.useRef<WebView>(null);
return (
<SafeAreaView style={styles.flexContainer}>
<WebView
ref={webViewRef}
source={{ uri: 'http://localhost:3000/' }}
startInLoadingState
javaScriptEnabled
allowsInlineMediaPlayback
style={styles.flexContainer}
onLoadEnd={() => webViewRef?.current?.postMessage("Hello")}
/>
</SafeAreaView>
);
};
And this is my React Code:
import React, { useState, useEffect, useRef } from 'react';
import './HolographicCard.css';
declare global {
interface Window {
ReactNativeWebView: {
postMessage(message: string): void;
};
}
}
const HolographicCard = () => {
const webViewRef = useRef<HTMLDivElement>(null);
useEffect(() => {
// Listen for messages from the WebView
const messageListener = (event: MessageEvent) => {
console.log('Message from WebView:', event.data);
};
window.addEventListener('message', messageListener);
return () => {
window.removeEventListener('message', messageListener);
};
}, []);
async function navigateBackToApp() {
window.ReactNativeWebView.postMessage('navigateBack');
}
return (
<div className="card-container">
<div className={`holographic-part`} ref={webViewRef}>
{/* ... other content ... */}
</div>
<button className="success-button" onClick={navigateBackToApp}>
Return
</button>
</div>
);
};
export default HolographicCard;
Use
injectJavaScriptto interact with web andpostMessagewith React Native. Here is a super simple example:See Communicating between JS and Native for details.