<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Süleyman Barış Eser on Medium]]></title>
        <description><![CDATA[Stories by Süleyman Barış Eser on Medium]]></description>
        <link>https://medium.com/@suleymanbariseser?source=rss-13764f67151a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Iu6_Y2g3raY9-pJU.jpg</url>
            <title>Stories by Süleyman Barış Eser on Medium</title>
            <link>https://medium.com/@suleymanbariseser?source=rss-13764f67151a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 09:43:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@suleymanbariseser/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to use React Native plugins inside WebView]]></title>
            <link>https://medium.com/@suleymanbariseser/how-to-use-react-native-plugins-inside-webview-3657bc13bc?source=rss-13764f67151a------2</link>
            <guid isPermaLink="false">https://medium.com/p/3657bc13bc</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[webview]]></category>
            <category><![CDATA[code]]></category>
            <dc:creator><![CDATA[Süleyman Barış Eser]]></dc:creator>
            <pubDate>Mon, 05 Sep 2022 15:46:43 GMT</pubDate>
            <atom:updated>2022-09-19T17:57:10.966Z</atom:updated>
            <content:encoded><![CDATA[<p>We have a web application with React and we want to create a new mobile application with React Native. We have a plan to use some of our existing React pages inside mobile application. We can use React pages inside WebView. However, we need to find a way to use native plugins inside web application.</p><p>Firstly, we need a communication bridge between native application and web application. With this bridge, we can send request from web application to native application and later, native application can send a response back. For example, web application needs to access storage (not local storage) so it sends a spesific message to native application and native application will read the key that passed with message and will return value to web application again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/481/1*y8Z0T3fW4-IGDUeFjf8PsA.png" /></figure><p>This approach requires two different implementation; one on web side and other on native side.</p><h3>Native application side</h3><p>We will use ref and onMessage properties of WebView component. Ref object will allow us to use WebView features like postMessage function to send data to web application. onMessage property of WebView will allow us to receive data from web application.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8dd23b865c2e622764519f23fbdec908/href">https://medium.com/media/8dd23b865c2e622764519f23fbdec908/href</a></iframe><p><em>If you work with android application and local server then, it would not access to localhost instead you need to pass a special ip address of the emulator which routes to localhost. You can check it in </em><a href="https://stackoverflow.com/a/4336952/14893702">https://stackoverflow.com/a/4336952/14893702</a></p><h3>Web application side</h3><p>Now, we can send data to web application and we can receive data from it. Let’s figure out it for web application too.</p><p>React Native WebView is adding a property to window object of web application and it is <strong><em>window.ReactNativeWebView.</em></strong> We can post messages to native application with this property.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef550ed6f005eb79f896955651698d44/href">https://medium.com/media/ef550ed6f005eb79f896955651698d44/href</a></iframe><p>We need a message structure to understand what is the operation we want to execute. We need a key property to define operation, a data property to pass values that would be useful on other side and lastly, a error property.</p><pre>interface MessageError {<br>  message: string;<br>  stack?: string;<br>}</pre><pre>interface MessageType&lt;K extends string, T&gt; {<br>  key: K;<br>  data: T;<br>  error?: MessageError;<br>}</pre><p>An example about sharing a link on web application;</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/92ea4141afd7ea63bff58a0cd19a792d/href">https://medium.com/media/92ea4141afd7ea63bff58a0cd19a792d/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ce886638f3be66be1b334696622b26d/href">https://medium.com/media/4ce886638f3be66be1b334696622b26d/href</a></iframe><p>As we are going to use more of this plugins, it would be great to add a new property in message type. This field will be named as idempotence and it will be unique to each call. With this field, we can wait for the response which will contain this unique id to understand if call is done or failed.</p><p>We will create a timer which will countdown from a specific time and after it will be done, we will remove listener and reject the promise. However, if we will receive the response before timer will be done, we can parse the response and resolve the promise.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d20dd70b5370353a00a72c00bede259/href">https://medium.com/media/9d20dd70b5370353a00a72c00bede259/href</a></iframe><p>We are almost there but there is a problem. We always used window object to receive responses and send requests in web application. However, weirdly webview in the android application will not receive the data if we use <strong>window.addEventListener</strong>, we need to use <strong>document.addEventListener</strong>. Now, we need to understand if we are in the ios application or android application. Of course, we can check user agent to understand the device but it would not be the best way. Instead, we can add a new window property to webview which will include platform of the device. <strong>injectedJavaScript</strong> can be used to add a new property to window object. In this case, we can pass this line of code to <strong>injectedJavaScript.</strong></p><pre>injectedJavaScript={`window.nativePlatform = &#39;${Platform.OS}&#39;;`}</pre><p>Now, we can use document or window according to platform.</p><pre>const receiver = window.nativePlatform === &#39;android&#39; ? document : window;</pre><pre>receiver.addEventListener(&#39;message&#39;, fn);<br>receiver.removeEventListener(&#39;message&#39;, fn);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/148/1*mc75JewLyh0E7Jlu5o-_8A.gif" /></figure><p><a href="https://github.com/suleymanbariseser/native-plugins-in-webapp">Here</a>, you can find full version of the project. This project can be integrated in web applications too by using iframe. There will just some small additions.</p><p>Thanks for your time!</p><h3><strong>References</strong></h3><ul><li>React Native WebView (<a href="https://github.com/react-native-webview/react-native-webview">https://github.com/react-native-webview/react-native-webview</a>)</li><li>WebView platform detection (<a href="https://stackoverflow.com/a/51032458/14893702">https://stackoverflow.com/a/51032458/14893702</a>)</li><li>Window-document event listeners (<a href="https://github.com/react-native-webview/react-native-webview/issues/356#issuecomment-466983107">https://github.com/react-native-webview/react-native-webview/issues/356#issuecomment-466983107</a>)</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3657bc13bc" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>