Write once, run anywhere with Create React (Native) App and react-native-web

October 09, 20185 min read

EDIT January 2018: I've updated this article with up to date versions (no more beta dependencies!). Thanks @KajiTetsushi for your help! 🙂

Since the first release of React Native, I’ve always tried to build a React codebase that could run on any platform.

Wow! Such engineering! Much platform! Amaze!

I loved building multi platform apps using Cordova but React Native has raised the bar so high now with its native performance.

So far, when you wanted to have a unified codebase of the Web and Native, you had to mess around with build systems (Webpack and React Native packager) in order to make everything work together.

Now with Create React Native App and Create React App, we do not have to worry about this layer of complexity.

Phase 1 : The dirty work

You’ll need :

  • the latest version of Node (when I wrote this post, it was 8.6.0)
  • create-react-app

First, create a React app using :

create-react-app my-hybrid-app && cd my-hybrid-app

We need to add a few dependencies to our project :

# npm install --save-dev
yarn add --dev babel-plugin-transform-object-rest-spread babel-plugin-transform-react-jsx-source babel-preset-expo jest-expo flow-bin react-native-scripts [email protected]

create-react-app installs [email protected]^16.2.0 and [email protected]^16.2.0.

To satisfy react-native's "absolute version" requirement, we need to downgrade react to v16.0.0 :

# npm uninstall --save
yarn remove react react-dom

and then

# npm install --save
yarn add [email protected] [email protected]

And now we add the required packages for react-native and expo :

# npm install --save
yarn add [email protected]^23.0.4 [email protected] react-native-web

Now let’s add a few files necessary to build a React Native app :

  • .babelrc
  "presets": ["babel-preset-expo"],
  "env": {
    "development": {
      "plugins": ["transform-object-rest-spread", "transform-react-jsx-source"]
  • .watchmanconfig
  • .flowconfig

Create a .flowconfig file in your project directory and add the flow settings available here

  • app.json
  "expo": {
    "sdkVersion": "23.0.0"
  • App.test.js : this is the entry point for testing the React Native app.
import React from 'react';
import App from './App';

import renderer from 'react-test-renderer';

it('renders without crashing', () => {
  const rendered = renderer.create(<App />).toJSON();

And then add a App.js file, this is going to be our React Native app entry point.

import React from 'react'
import App from './src/App'

export default class NativeApp extends React.Component {
  render() {
    return <App />

Then, let’s make a few changes to our package.json file and add some useful scripts :

// These scripts are merely copied from the create-react-native-app package.json file
"main": "./node_modules/react-native-scripts/build/bin/crna-entry.js",
"scripts": {
  "start-web": "react-scripts start",
  "build-web": "react-scripts build",
  "test": "react-scripts test --env=jsdom",
  "eject-web": "react-scripts eject",

  "start-native": "react-native-scripts start",
  "eject-native": "react-native-scripts eject",
  "android": "react-native-scripts android",
  "ios": "react-native-scripts ios",
  "test-native": "node node_modules/jest/bin/jest.js --watch",

  "test": "npm run test-web && npm run test-native",
"jest": {
  "preset": "jest-expo"

Phase 2 : Now let’s profit!

Everything is set up in our project, let’s start both packagers in two different terminal windows.

Terminal 1 :

npm run start-web

Terminal 2 :

npm run ios

We need to edit ./src/App.js and add some platform agnostic code in it.

You can now write all your code as if you were writing some React Native code.

The trick here is that the webpack config of Create React App aliases automatically react-native to react-native-web (see the config here).

Everything is handled for us!

import React, { Component } from 'react'
import { View, Text, StyleSheet } from 'react-native'

export default class App extends Component {
  render() {
    return (
      <View style={styles.app}>
        <View style={styles.appHeader}>
          <Text style={styles.appTitle}>Welcome to React ⚛️</Text>
        <Text style={styles.appIntro}>
          To get started, edit src/App.js and save to reload.

const styles = StyleSheet.create({
  app: {
    flex: 1
  appHeader: {
    flex: 1,
    backgroundColor: '#222',
    padding: 20,
    justifyContent: 'center',
    alignItems: 'center'
  appTitle: {
    fontSize: 16,
    color: 'white'
  appIntro: {
    flex: 2,
    fontSize: 30,
    textAlign: 'center'

You should now see this :

Fig 1

The result on the web is not really the same as on the mobile. That’s because the #root DOM element styling needs to be tweaked to match its mobile counterpart :

To do so, open ./src/index.css and add

/* ... */

#root {
  display: flex;
  flex-direction: column;
  min-height: 100vh;

And Tada!

Fig 2

You’ve built your first universal React components running on the Web, iOS and Android.

I did not invent anything here : I leveraged the amazing work done by the teams behind Create React App, Create React Native App, react-native-web and Expo.

You should check these projects out and support them! They help making React development a breeze!

author undefined

By Yannick Spark : a Front-end engineer who works remotely at Teacup Analytics.
He likes Functional programming, Nutrition & Fasting, and Remote work.
You should definitely on Twitter 👋