<?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 Guy Blank on Medium]]></title>
        <description><![CDATA[Stories by Guy Blank on Medium]]></description>
        <link>https://medium.com/@blank.guy?source=rss-94326182e6b8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*v8fazXZb7nU9NNRAIL6bZQ.jpeg</url>
            <title>Stories by Guy Blank on Medium</title>
            <link>https://medium.com/@blank.guy?source=rss-94326182e6b8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 09:11:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@blank.guy/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[Extending React Native’s Dev Menu]]></title>
            <link>https://medium.com/capriza-engineering/extending-react-natives-dev-menu-c084fc93717d?source=rss-94326182e6b8------2</link>
            <guid isPermaLink="false">https://medium.com/p/c084fc93717d</guid>
            <category><![CDATA[devtools]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Guy Blank]]></dc:creator>
            <pubDate>Wed, 22 Nov 2017 19:00:32 GMT</pubDate>
            <atom:updated>2017-11-22T19:00:32.068Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/578/1*zrYi_D48A6pWcqUr52IhNg.png" /></figure><p>One of the cool things in the react native framework is that it comes with integrated <a href="https://facebook.github.io/react-native/docs/debugging.html#accessing-the-in-app-developer-menu">dev tools</a>. Though I’m not a big fan of the shake gesture, I find myself using the development menu a lot, both on the simulator/emulator and on real devices.</p><p>But what about adding your own development tools in the react native development menu, tools that are relevant to your specific domain?</p><p>Although I couldn’t find how to do this in react native’s documentation, sieving through the source code it appears that the development menu has an API for custom menu entries — both for Android and for iOS!</p><p>I’ve put together a quick, 2-step how-to guide and uploaded a <a href="https://github.com/blankg/rn-devmenu-ext">sample project</a> for reference.</p><ul><li>First we’ll just add custom dev menu entries to both iOS &amp; Android which don’t do much except write to the log</li><li>Next, since we are in react native, we would like to call the JavaScript side and toggle a react native UI in response to a menu item click</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*k7oh5IYRJreU7g_YMd5tUg.gif" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*yR6GmmjOIE3Ut0knS33rxg.gif" /><figcaption>React Native dev menus with custom entries in Android &amp; iOS</figcaption></figure><h3>The iOS Menu Item</h3><p>Let’s create a new <a href="http://facebook.github.io/react-native/docs/native-modules-ios.html">native module</a> with an <em>RCTDevMenuItem</em> property which will be our custom entry.</p><pre>@interface CustomDevMenuModule : NSObject &lt;RCTBridgeModule&gt;</pre><pre>#if __has_include(&quot;RCTDevMenu.h&quot;)</pre><pre>@property (nonatomic, strong, readonly) RCTDevMenuItem *devMenuItem;</pre><pre>#endif</pre><pre>@end</pre><p>We’ll <em>synthesize</em> the bridge property and override its setter in-order to use the <em>[bridge.devMenu addItem:]</em> api to add our custom dev menu. <br>Next, we override the <em>devMenuItem</em> getter to instantiate a “My Test” menu item that will write to the log on click.</p><blockquote>Note: we are implementing a bridge module here simply to get a reference to the bridge instance. If you already have a reference to a bridge instance you can add the dev menu item there.</blockquote><pre>@implementation CustomDevMenuModule</pre><pre>@synthesize bridge = _bridge;</pre><pre>#if __has_include(&lt;React/RCTDevMenu.h&gt;)</pre><pre>RCTDevMenuItem *_devMenuItem;</pre><pre>#endif</pre><pre>RCT_EXPORT_MODULE();</pre><pre>...</pre><pre>- (void)setBridge:(RCTBridge *)bridge<br>{<br>    _bridge = bridge;</pre><pre>#if __has_include(&lt;React/RCTDevMenu.h&gt;)</pre><pre>    [_bridge.devMenu addItem:self.devMenuItem];</pre><pre>#endif</pre><pre>}</pre><pre>#if __has_include(&lt;React/RCTDevMenu.h&gt;)</pre><pre>- (RCTDevMenuItem *)devMenuItem<br>{<br>    if (!_devMenuItem) {<br>        _devMenuItem = <br>            [RCTDevMenuItem buttonItemWithTitleBlock:^NSString *{<br>                return @&quot;My Test&quot;;} handler:^{<br>                    NSLog(@&quot;My Test Clicked!&quot;);}];<br>    }<br>    return _devMenuItem;<br>}</pre><pre>#endif</pre><pre>@end</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*kxWSc5y_i_EkIbHWrkLAJA.png" /></figure><h3>The Android Menu Item</h3><p>Let’s create a <em>CustomDevOptionsHandler</em> class which implements the <em>DevOptionHandler</em> interface.</p><pre><strong>public class </strong>CustomDevOptionsHandler <strong>implements </strong>DevOptionHandler {<br><br>    @Override<br>    <strong>public void </strong>onOptionSelected() {<br>        Log.<em>d</em>(<strong>&quot;CustomDevOptionsHandler&quot;</strong>, <strong>&quot;My Test Clicked!&quot;</strong>);<br>    }<br>}</pre><p>In the <em>MainActivity,</em> use the <em>DevSupportManager.addCustomDevOption</em> API (which we get from the <em>ReactInstanceManager</em>) to add our <em>CustomDevOptionsHandler</em>.</p><pre><strong>public class </strong>MainActivity <strong>extends </strong>ReactActivity {<br><br>    ...<br><br>    @Override<br>    <strong>protected void </strong>onCreate(Bundle savedInstanceState) {<br>        <strong>super</strong>.onCreate(savedInstanceState);<br>        getReactInstanceManager().getDevSupportManager()<br>        .addCustomDevOption(<strong>&quot;My Test&quot;</strong>,<br>              <strong>new </strong>CustomDevOptionsHandler());<br>    }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TkQ6GDqBjJpOLElW97mfHg.png" /></figure><p>We have just created a custom dev menu entry in both iOS and Android!</p><p>If you just want to call a native API or show some native UI, you can go ahead and extend the sample above (you can find the full code <a href="https://github.com/blankg/rn-devmenu-ext">here</a>).</p><h3>Click Handling — Native Side</h3><p>Next lets see how to call react native and display a react native view on click.</p><p>The way native code calls JavaScript in react native is with <a href="https://facebook.github.io/react-native/docs/native-modules-ios.html#sending-events-to-javascript">events</a> — so that’s exactly what we’ll do. The dev menu item click handler will trigger an event to the JavaScript side that in turn will toggle a view.</p><p>First we implement an EventEmitter bridge module:</p><pre>@interface CustomDevMenuEventEmitter : RCTEventEmitter &lt;RCTBridgeModule&gt;</pre><pre>@end</pre><pre>@implementation CustomDevMenuEventEmitter</pre><pre>RCT_EXPORT_MODULE();</pre><pre>+ (BOOL)requiresMainQueueSetup<br>{<br>    return NO;<br>}</pre><pre>- (dispatch_queue_t)methodQueue<br>{<br>    return dispatch_get_main_queue();<br>}</pre><pre>- (NSArray&lt;NSString *&gt; *)supportedEvents<br>{<br>    return @[@&quot;toggleTestView&quot;];<br>}</pre><pre>- (void)receiveTestNotification<br>{<br>    [self sendEventWithName:@&quot;toggleTestView&quot; body:nil];<br>}</pre><pre>- (void)startObserving<br>{<br>    [[NSNotificationCenter defaultCenter] addObserver:self<br>        selector:@selector(receiveTestNotification) <br>        name:@&quot;toggleTestView&quot; object:nil];<br>}</pre><pre>- (void)stopObserving<br>{<br>    [[NSNotificationCenter defaultCenter] removeObserver:self];<br>}</pre><pre>@end</pre><p>Our event emitter, which fires a “toggleTestView” event, is triggered by an iOS notification with the same name that we’ll fire from our (previously created) <em>CustomDevMenuModule</em>:</p><pre>@implementation CustomDevMenuModule</pre><pre>...</pre><pre>#if __has_include(&lt;React/RCTDevMenu.h&gt;)</pre><pre>- (RCTDevMenuItem *)devMenuItem<br>{<br>    if (!_devMenuItem) {<br>        _devMenuItem = <br>            [RCTDevMenuItem buttonItemWithTitleBlock:^NSString *{ <br>                return @&quot;My Test&quot;;} handler:^{ <br>                    NSLog(@&quot;My Test Clicked!&quot;);   <br>                    [[NSNotificationCenter defaultCenter] <br>                        postNotificationName:@&quot;toggleTestView&quot; <br>                            object:nil];}];<br>    }<br>    <br>    return _devMenuItem;<br>}</pre><pre>#endif</pre><pre>@end</pre><p>In Android things are a bit simpler — we’ll just fire the event directly from our <em>CustomDevOptionsHandler.</em> The only thing missing is that we need a reference to the <em>ReactInstanceManager</em>, so we’ll modify our code to pass in a reference from the <em>MainActivity:</em></p><pre><strong>public class </strong>MainActivity <strong>extends </strong>ReactActivity {</pre><pre>...<br>    @Override<br>    <strong>protected void </strong>onCreate(Bundle savedInstanceState) {<br>        <strong>super</strong>.onCreate(savedInstanceState);<br>        getReactInstanceManager().getDevSupportManager()<br>        .addCustomDevOption(<strong>&quot;My Test&quot;</strong>,<br>                <strong>new </strong>CustomDevOptionsHandler(<br>                        getReactInstanceManager()));<br>    }<br>}</pre><p>and in the <em>CustomDevOptionsHandler:</em></p><pre><strong>public class </strong>CustomDevOptionsHandler <strong>implements </strong>DevOptionHandler {<br><br>    <strong>private </strong>ReactInstanceManager <strong>_instanceManager</strong>;<br><br>    <strong>public </strong>CustomDevOptionsHandler(ReactInstanceManager instanceManager) {<br>        <strong>this</strong>.<strong>_instanceManager </strong>= instanceManager;<br>    }<br><br>    @Override<br>    <strong>public void </strong>onOptionSelected() {<br>        Log.<em>d</em>(<strong>&quot;CustomDevOptionsHandler&quot;</strong>, <strong>&quot;My Test Clicked!&quot;</strong>);<br>        <strong>this</strong>.<strong>_instanceManager<br>        </strong>.getCurrentReactContext()<br>        .getJSModule(DeviceEventManagerModule<br>                .RCTDeviceEventEmitter.<strong>class</strong>)<br>        .emit(<strong>&quot;toggleTestView&quot;</strong>, <strong>null</strong>);<br>    }<br>}</pre><h3>Click Handling — JavaScript Side</h3><p>Let’s create a component that listens to the “toggleTestView” event and toggles a modal view:</p><pre><strong>export default class </strong>CustomDevMenuView <strong>extends </strong>Component {<br>  constructor(props) {<br>    <strong>super</strong>(props);<br><br>    <strong>this</strong>.state = {<br>      modalVisible: <strong>false<br>    </strong>};<br>  }<br><br>  componentWillMount() {<br>    <strong>this</strong>.subscription = <strong>new </strong>NativeEventEmitter(<br>      NativeModules.CustomDevMenuEventEmitter<br>    ).addListener(&quot;toggleTestView&quot;, () =&gt; {<br>      <strong>this</strong>.setState({ modalVisible: !<strong>this</strong>.state.modalVisible });<br>    });<br>  }<br><br>  componentWillUnmount() {<br>    <strong>this</strong>.subscription.remove();<br>  }<br><br>  render() {<br>    <strong>return </strong>(<br>      &lt;Modal<br>        animationType=&quot;slide&quot;<br>        transparent={<strong>false</strong>}<br>        visible={<strong>this</strong>.state.modalVisible}<br>        onRequestClose={() =&gt; <strong>this</strong>.setState({ modalVisible: <strong>false </strong>})}<br>      &gt;<br>        &lt;View style={styles.devMenuContainer}&gt;<br>          &lt;Text style={styles.welcome}&gt;Hello custom dev menu!&lt;/Text&gt;<br>        &lt;/View&gt;<br>      &lt;/Modal&gt;<br>    );<br>  }<br>}</pre><p>Add the component to our app (note that it only renders in dev mode):</p><pre><strong>export default class </strong>App <strong>extends </strong>Component&lt;{}&gt; {<br>  render() {<br>    <strong>return </strong>(<br>      &lt;View style={styles.container}&gt;<br>        {__DEV__ ? &lt;CustomDevMenuView /&gt; : <strong>null</strong>}<br>        &lt;Text style={styles.welcome}&gt;Welcome to React Native!&lt;/Text&gt;<br>        &lt;Text style={styles.instructions}&gt;To get started, edit App.js&lt;/Text&gt;<br>        &lt;Text style={styles.instructions}&gt;{instructions}&lt;/Text&gt;<br>      &lt;/View&gt;<br>    );<br>  }<br>}</pre><p>And that’s it! We now have custom dev menu entries that toggle a react native view.</p><p>I hope you can use this example to create tools that help you and your team in your react native development.</p><p>Full code is available <a href="https://github.com/blankg/rn-devmenu-ext">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c084fc93717d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/capriza-engineering/extending-react-natives-dev-menu-c084fc93717d">Extending React Native’s Dev Menu</a> was originally published in <a href="https://medium.com/capriza-engineering">Capriza Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Communicating between React Native and the WebView]]></title>
            <link>https://medium.com/capriza-engineering/communicating-between-react-native-and-the-webview-ac14b8b8b91a?source=rss-94326182e6b8------2</link>
            <guid isPermaLink="false">https://medium.com/p/ac14b8b8b91a</guid>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[webview]]></category>
            <category><![CDATA[mobile]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Guy Blank]]></dc:creator>
            <pubDate>Mon, 07 Aug 2017 06:38:16 GMT</pubDate>
            <atom:updated>2018-08-28T06:19:08.788Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-e6asA4andgWfippBjFbEw.png" /></figure><p>Our current WorkSimple app in production is a Cordova hybrid app which means that our UI is web based. We are putting in a lot of effort trying to make the user experience as smooth as possible, but it never feels as good as native apps.</p><p>For this reason, we have been pushing for React Native for a while. Once we received the go-ahead we decided that our first milestone would be to get rid of Cordova and its plugins but stay inside the WebView running within a React Native wrapper, and in the next milestone we plan on migrating our app’s React.js components to React Native.</p><p>We are happy to share some of what we have learned during the process, with a focus on working with the React Native WebView.</p><p>The first and most basic task we had was to find a way to communicate between the React Native code and the web code running in the WebView. There are three React Native WebView APIs that can be used (not including some more ways that require writing native code):</p><ol><li><strong>injectedJavaScript</strong>: lets you inject JavaScript code to be executed within the context of the WebView. <br>The problem is that this code will only run once the WebView is loaded so unless you want to reload your web content each time you want to pass something into the WebView this is not the way to go. This would be more suitable for some one time onLoad code. <br>Another strange thing is that it behaves differently between iOS and Android — in iOS you can get the returned value from the injected JavaScript (as long as it’s a string), but in Android, well… you get nothing.</li></ol><pre>&lt;WebView<br>    source={require(&#39;./resources/index.html&#39;)}<br>    injectedJavaScript={&#39;(function(){return &quot;Send me back!&quot;}());&#39;}<br>    onNavigationStateChange={(navEvent)=&gt; console.log(navEvent.jsEvaluationValue)}<br>/&gt;</pre><p>2. <strong>injectJavaScript</strong>: lets you inject JavaScript code that is executed immediately on the WebView, with no return value. We found this API to be perfect for setting globals and dispatching events on the window.</p><p><strong>3. onMessage/postMessage</strong>: this is probably the <em>only</em> API that gives a real two way bridge for communication. <strong>postMessage</strong> does in its implementation something similar to <strong>injectJavaScript, </strong>only with the extra boilerplate code of firing a message event in the WebView. <br>Sending messages between React Native and the WebView is a matter of calling <strong>postMessage</strong> and implementing <strong>onMessage</strong> on the receiving side to get the message:</p><pre>&lt;WebView<br>    source={require(&#39;./resources/index.html&#39;)}<br>    onMessage={(event)=&gt; console.log(event.nativeEvent.data)}<br>/&gt;</pre><p>This seems simple enough, but if you’re planning on heavy usage of this communication channel you should be aware of a few caveats:</p><ol><li>There is no out of the box response mechanism for the postMessage calls (on both sides) so you would probably need to have some components that can manage some sort of callback mechanism. You can take a look at the <a href="https://gist.github.com/blankg/d5537a458b55b9d15cb4fd78258ad840">WebViewBridge example</a> for a sample implementation of such a component for the code running in the WebView.</li><li>There is an issue in iOS when doing consecutive window.postMessage calls. The way React-Native implemented the <strong>window.postMessage</strong> in iOS is by setting a URL with a special schema and containing the data posted as a url parameter. Then in the native code, they listen for url changes and look for ones with the postMessage schema to extract the data and pass it to the onMessage handlers. The problem is that doing consecutive window.postMessage calls in iOS will result in <em>losing your first calls</em> as the url is overridden each time before the message is delivered so the last call wins. Luckily, <a href="https://github.com/facebook/react-native/pull/11304">a fix for this issue</a> is on its way. In the mean time, you can checkout the <a href="https://gist.github.com/blankg/d5537a458b55b9d15cb4fd78258ad840">WebViewBridge sample</a> which handles this issue with a promise chain that resolves each time a message gets delivered.</li></ol><p>Another concern we initially had with the implementation on iOS was that since the data is actually posted on the url string, can it handle large data? We tested this by passing a 20MB image file encoded as a base64 string — turns out iOS has no problem passing that successfully. <br>But be careful, do not do this in debug mode. When we accidentally logged the url containing this massive string to the console, the app froze up entirely — our guess is that since it is transmitted over a websocket to the chrome browser, the debug channel gets jammed.</p><p>As of now we’ve completely replaced all of the Cordova code with React Native, still utilizing a WebView to host our app. The choice of using postMessage/onMessage for communicating between React Native and the WebView has turned out to work quite well.</p><p><strong>Update 1</strong>: A question that keeps coming up from people who read the post is :”<em>Did you get a big performance boost?</em>”</p><p>Well getting the big performance boost wasn’t the goal of the our first mile stone in the migration to React-Native (since most of our UI &amp; logic still live inside the WebView). If you take into account that in our pure Cordova app, calls to native used to go over the WebView bridge straight into the native code where in our current state in our React-Native app calls go over the WebView bridge and then probably (if native code is needed) over the React-Native bridge as well. So we might even experience some performance degradation at this stage.</p><p>But the Cordova FW and plugins are huge and not having to load all that code and wait for device ready did give us an improvement in load time.</p><p>Another benefit was that some Cordova code that had implementations in both iOS and Android we were able to replace with a single React-Native javascript implementation which improved our TCD.</p><p><strong>Update 2:</strong> <a href="http://facebook.github.io/react-native/blog/2018/08/27/wkwebview">React Native 0.57 moves to WKWebView!</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ac14b8b8b91a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/capriza-engineering/communicating-between-react-native-and-the-webview-ac14b8b8b91a">Communicating between React Native and the WebView</a> was originally published in <a href="https://medium.com/capriza-engineering">Capriza Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>