No Description

Messaging.tsx 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import React, {Component} from 'react';
  2. import {Text, View, Alert} from 'react-native';
  3. import WebView from 'react-native-webview';
  4. const HTML = `
  5. <!DOCTYPE html>\n
  6. <html>
  7. <head>
  8. <title>Messaging</title>
  9. <meta http-equiv="content-type" content="text/html; charset=utf-8">
  10. <meta name="viewport" content="width=320, user-scalable=no">
  11. <style type="text/css">
  12. body {
  13. margin: 0;
  14. padding: 0;
  15. font: 62.5% arial, sans-serif;
  16. background: #ccc;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <button onclick="sendPostMessage()">Send post message from JS to WebView</button>
  22. <p id="demo"></p>
  23. <script>
  24. function sendPostMessage() {
  25. window.ReactNativeWebView.postMessage('Message from javascript');
  26. }
  27. </script>
  28. </body>
  29. </html>
  30. `;
  31. type Props = {};
  32. type State = {};
  33. export default class Alerts extends Component<Props, State> {
  34. state = {};
  35. render() {
  36. return (
  37. <View style={{height: 120}}>
  38. <WebView
  39. source={{html: HTML}}
  40. automaticallyAdjustContentInsets={false}
  41. onMessage={(e: {nativeEvent: {data?: string}}) => {
  42. console.log('onMessage', e.nativeEvent.data);
  43. Alert.alert('Message received', e.nativeEvent.data);
  44. }}
  45. />
  46. </View>
  47. );
  48. }
  49. }