React Native for Web – Write Once, Run Anywhere
Tags: react, reactnative, mobile, webdev
Published: 2020-08-27 13:11:14

<p><a rel="noreferrer noopener" href="https://pagepro.co/services.html" target="_blank">Web and mobile development</a> is growing and changing very dynamically.</p>
<p>These two are separate worlds, but they have a lot in common, and they are crossing constantly. Sometimes we are facing the problem of running the app in a different environment. We can handle Android and iOS devices with <a href="https://reactnative.dev/" target="_blank" rel="noreferrer noopener nofollow">React Native</a>, but we actually have to keep another codebase for the web.</p>
<p>What if I tell you there is a remedy for that?</p>
<p>What if you can create an app that is platform agnostic and run it no matter if it’s a mobile device or browser?</p>
<p>Thanks to React Native for Web it’s possible!</p>
<h2><b>What is React Native for Web?</b></h2>
<p><a href="https://github.com/necolas/react-native-web" target="_blank" rel="noreferrer noopener nofollow">React Native for Web</a> was initially created by Nicolas Gallagher, former Twitter employee, to create the Twitter Lite PWA. In this case, it makes it possible to run <a href="https://pagepro.co/blog/what-is-react-native-a-definite-guide-to-the-top-mobile-framework/" target="_blank" rel="noreferrer noopener">React Native</a> components and APIs on the web.</p>
<p>Wait, what?</p>
<p>Isn’t it React DOM’s job to make things work on the web?</p>
<p>Why should we use anything that was originally created for another purpose?</p>
<p>The answer is: because React Native for Web gives us something extra.</p>
<p>It allows us to create an application that runs on Android, iOS and browsers, sharing just one codebase.</p>
<p>How cool is that?</p>
<h2><b>How React Native for Web works?</b></h2>
<p>We should consider React Native as a pure <strong>UI language</strong>. It contains some basic components that define UI primitives.</p>
<p>What are the primitives? It’s a set of building blocks for the UI. </p>
<p>In React Native, all the components we can create are based on primitives like “<em>&lt;View/&gt;</em>” or “<em>&lt;Image/&gt;</em>“. They represent elements that make sense for every visual interface, no matter where it’s run. The core difference between React Native primitives and DOM primitives is the fact that DOM primitives are meant to create some structure of the HTML, and React Native primitives are just describing the UI.</p>
<p>With that knowledge, let’s take a deep dive into the “<em>&lt;Text/&gt;</em>” primitive.</p>
<p>Depending on the mobile platform, React Native is providing us with native functions and views, in this case “<em>TextView</em>” on Android and “<em>UILabel</em>” on iOS.</p>
<p>Knowing that React Native can define some UI primitives, it is possible to transform them to the DOM language by using HTML tags.</p>
<p>This is where React Native for Web comes in, and allows you to render the “<em>&lt;Text/&gt;</em>” as “<em>&lt;span/&gt;</em>” in the browser!</p>
<h2><b>Why using React Native for Web?</b></h2>
<p>If you ever worked with projects that share a lot of stuff between each other, you might love the core advantage of this library: you can write your code just once, and share it across multiple platforms.</p>
<p>The other big advantage of this solution are native-quality interactions. No matter if you are using it on your personal computer, browser in your smartphone, or native version of the app, you have the support for multiple input modes, like touch, mouse or keyboard. For example, if you create a <em>&lt;Button/&gt;</em> with an <em>onLongPress</em> property, it will be handled on all the platforms.</p>
<p>Another good news is support for accessibility. React Native for Web includes APIs that help developers to create more accessible apps. The best supported accessibility features of the web are exposed: <em>accessible</em>, <em>accessibilityLabel</em>, <em>accessibilityLiveRegion</em>, <em>accessibilityRole</em>, and <em>importantForAccessibility</em>. Why should we care about this? To make sure every single user of our app has equal access to it.</p>
<p>There are also some other features that might help developers create great universal apps, like RTL support. Application layout can be automatically flipped to support right-to-left languages.</p>
<p>It’s also worth mentioning that React Native for Web supports server side rendering, and you can integrate it with some popular tools like Gatsby or Next. If you are interested in that, you should check the examples in React Native for Web repository on Github.</p>
<p>To sum it up, <strong>React Native for Web advantages</strong> are:</p>
<ul><li>one codebase to share across platforms and devices</li><li>native-quality interactions</li><li>support for accessibility</li><li>RTL support</li><li>Server Side Rendering Support</li><li>Integration with Static pages</li><li>and more interesting features</li></ul>
<h2><b>How to use it?</b></h2>
<p>If you want to try it, I truly recommend using Expo. From SDK version 33, the projects bootstrapped with the Expo CLI have had web support from the start. It’s also possible to use a well-known create-react-app, or even running it with your standalone app with webpack and babel.</p>
<p>As I mentioned earlier, the library allows us to use React Native components and APIs across the web. Let’s take a look at the possible use cases of APIs.</p>
<p>One of the most popular of them are StyleSheet and Platform.</p>
<p>The first one allows us to create css-like styles in the native app, and the second one helps with code execution based on the platform. The great news is that we can use both of them also with React Native Web:</p>
```javascript
import {Platform, StyleSheet} from ‘react-native’;

const styles = StyleSheet.create({
    container: {
        height: Platform.OS === 'web' ? 250 : 100
    }
});
```
<p>It’s also possible to import the whole different file based on the platform, and for that you can use it as in the example below:</p>
```javascript
ComponentName.android.js
ComponentName.ios.js
ComponentName.web.js
```
<p>There is also support for other APIs that you can use across all the platforms, like <em>Linking</em>, <em>Dimensions</em>, or <em>AppState</em>.</p>
<p>When it comes to components, most of them are ready to use on the web. Just a few of them are not finished yet (June 2020), like <em>Modal</em>, <em>RefreshControl</em>, and <em>TouchableNativeFeedback</em>, and some of them are just mocked because of lack of the equivalent on the web API, like <em>KeyboardAvoidingView</em> and <em>StatusBar</em>.</p>
<p>Some popular UI kits are also adopted to use on the web. If you are looking for a ready-to-go set of the UI components you can share across all platforms, you can try UI Kitten or Nativebase. They have really good support for the web and if you need them, you can easily set them up with your project.</p>
<p>If you want to quickly create an app with a few screens, I have another great news for you: <em>react-navigation</em> is also supporting the web! It’s still experimental, requires lots of improvements from the library developers, but it works on the browser.</p>
<p>If you are running your project with Expo, you can use a lot of goodies the Expo SDK is providing. A lot of them are compatible with all the platforms and can save you hours of coding. For example, you can use Camera or Location with just a few lines of code and you’ll be able to handle them no matter the platform is.</p>
<p>I personally think Expo has a great support for universal apps and it’s the best way to start building them from scratch. But to actually do this, we have to ask ourselves probably the most important question when considering using React Native for Web.</p>
<h2><b>Are there any limitations?</b></h2>
<p>And unfortunately, the answer is: yes, there are.</p>
<p>And they can make you decide that it’s not the technology that fits perfectly to your project.</p>
<p>Why? Because if you know that your project will contain some libraries that rely on native dependencies, they won’t work on the web. And even if you use only libraries that are free of native dependencies, you can’t expect them to work 100%.</p>
<p>You should also keep in mind that not all the React Native APIs are available in the browser. Some of them are still in progress, and some don’t have an equivalent in the browser.</p>
<p>And don’t forget that even if mobile and browser have some things in common, they are still different environments, so you may end up creating a lot of platform-based conditions to handle all the things correctly.</p>
<h2><b>What does it mean for business?</b></h2>
<p>There is still a lot that RNfW must go through, but the future is incredibly interesting and seems bright.</p>
<p>Technically we’ve learned a lot, but is there any impact that React Native for Web may have on the business side?</p>
<p>Why would any CEO, COO, CMO, CSO, CFO. give a green light to invest in it? What’s the future?</p>
<h3><b>High-quality Web Interface</b></h3>
<p>This enables you to:</p>
<ul><li>Build complex apps easier</li><li>Enhance User Experience largely</li><li>Perform better SEO-wise</li><li>Execute the interface the way you truly want it</li></ul>
<h3><b>Write once, render anywhere</b></h3>
<p>And this enables you to:</p>
<ul><li>Build app that will work on iOS, Android, and any device simultaneously</li><li>Shorten your MVP time-to-market</li><li>Save (time and money) on development</li><li>Save (time and money) on maintenance</li><li>Build truly futureproof app</li></ul>
<p>There are even more interesting reasons to use React, and reasons to use React Native that you may find inspirational while choosing your tech stack.</p>
<h2><b>Conclusions</b></h2>
<p>Is it worth using React Native for Web?</p>
<p>If it fits your project and your requirements as a developer, I think it’s a good way to go.</p>
<p>As you can see above, there are some cons of this solution, so the key to success is to recognize if they are not affecting your project.</p>
<p>I think React Native for Web is a big thing and it may revolutionize the way we create universal apps. With the amazing support of Expo, it can save developers a lot of hours of work, and most importantly, save the companies a lot of money.</p>