Recognized by Clutch.co as a top-rated Mobile App Development Company.
folio3-mobile
US 408 365 4638
START YOUR PROJECT
  • Solutions
    • Apps Discovery Services
    • Team Augmentation
    • Enterprise
    • AR/VR
    • IoT
    • Wearables
    • Field Sales
    • On Demand Apps
  • Industries
    • Retail
    • Agriculture
    • Healthcare
    • Pharmaceutical & Life Sciences
    • Manufacturing
    • Automotive
    • Logistics
    • Education
  • Technologies
    • Native Mobile Apps
      • iOS
      • Android
    • Cross Platform Apps
      • React Native
      • Flutter
      • Ionic
      • Xamarin
      • NativeScript
      • Sencha
  • Portfolio
  • Blog
  • Contact Us
  • Solutions
    • Apps Discovery Services
    • Team Augmentation
    • Enterprise
    • AR/VR
    • IoT
    • Wearables
    • Field Sales
    • On Demand Apps
  • Industries
    • Retail
    • Agriculture
    • Healthcare
    • Pharmaceutical & Life Sciences
    • Manufacturing
    • Automotive
    • Logistics
    • Education
  • Technologies
    • Native Mobile Apps
      • iOS
      • Android
    • Cross Platform Apps
      • React Native
      • Flutter
      • Ionic
      • Xamarin
      • NativeScript
      • Sencha
  • Portfolio
  • Blog
  • Contact Us

Sharing Code Between React And React Native

Published by: Muhammad Saqlain | January 8, 2020 msaqlain
SCROLL AND BE AMAZED!
Home > Blog > Sharing Code Between React And React Native

Code reuse intends to save time and resources, and reduce redundancy by taking benefit of assets that have already been designed or developed in some form within the software product development process. You can reuse a lot of code between React and React Native e.g. business logic, utilities, helpers, API fetching logic, etc. But don’t overreach on what you’re sharing because doing so may leave your code harder to maintain.

UI components will have to be written separately for both mobile and web.

Contents of ‘shared’ folder:

  • Business logic
  • Communication with API
  • State management classes: Stores, Reducers, Actions
  • Helpers
  • Constants
  • Storage Services
  • HOCs (Higher-Order Components)

Contents of ‘web’ & ‘mobile’ folder:

  • Presentational components
  • Navigation / Routing
  • Styles

Setting up a shared project

Open terminal. Make sure you are in the project root folder.

  • Run the following command to create the mobile, web, and shared folders:
$ mkdir -p packages/shared/src packages/mobile packages/web
  • Create a new React Native project using react-native-cli inside packages/mobile
  • Create a new React app using create-react-app inside packages/web
  • Create a ‘package.json‘ file in the root directory to enable yarn workspaces and paste the following:
{
    "name": "monorepo",
    "private": true,
    "workspaces": {
        "packages": [
            "packages/*"
        ],
        "nohoist": []
    },
    "dependencies": {
        "react-native": "0.69.5"
    }
}

Create a shared folder

Now create a shared folder where the shared code of the React and React Native apps will reside.

$ mkdir -p packages/shared
  • Create a ‘package.json‘ file inside the shared folder and paste the following:
{
    "name": "@monorepo/shared",
    "version": "1.0.0",
    "dependencies": {}
}

Directory Structure

This image has an empty alt attribute; its file name is alLsg6VOeJDN31UFwTNrTTGR6p_DV7wDA0VKDE0v23ttoctApbkD2uIQhIFc8aY-jXCrglo_wr4x5Q2C4X4h-nZgBaD2bk2RGvU3xqz8Tb6NzL3rbLQTwBa9gH9-XEdqmwiQ3Z4QEGM

Configure the web (React) application

  • In your terminal, change the directory to packages/web and run the following commands:
npm install react-app-rewire-yarn-workspaces react-app-rewired --save-dev
  • Replace the scripts section in the package.json file with the following:
"scripts": {
    "start": "react-app-rewired start",
    "build": "react-app-rewired build",
    "test": "react-app-rewired test --env=jsdom",
    "eject": "react-app-rewired eject"
}
  • Create a new file named ‘config-overrides.js' inside web folder and paste the following:
const rewireYarnWorkspaces = require("react-app-rewire-yarn-workspaces");

module.exports = function override(config, env) {
    return rewireYarnWorkspaces(config, env);
}
  • Now run the following command to install the dependencies:
yarn install

Configure the mobile (React Native) application

Configuring a React Native app through monorepo is a bit tricky. Firstly, change the directory to packages/mobile on your terminal. Now, you will need to understand two things before making workspaces work.

  1. No Hoist
  2. Symlinking

1. No Hoist

Inside the package.json file, the packages that you list down under “nohoist” tag will only be available for mobile and will not be hoisted globally.

2. Symlinking

A symlink is a term used for any file that contains a reference to another file or package. To achieve symlinking, we will use ‘wml’.

  • Install wml globally using the following command:
npm install -g wml
  • Copy the by running the following command in the root directory:
wml add packages/shared packages/mobile/node_modules/@monorepo/shared
  • Start wml using the following command:
wml start

Once you run the above command, you should see an output something like this:

In case no output gets displayed, then follow this answer to get the issue fixed: https://github.com/wix/wml/issues/38#issuecomment-521570796

You can now develop and import functions from the shared folder inside the components in the web and mobile folders. Add a file inside packages/shared/src folder and export a function inside it. You should then be able to import the function inside your web and mobile files like this:

import { functionName } from '@monorepo/shared/src/fileName';

This is how we set up a project to share code between React and React Native applications. Thanks for reading!


About msaqlain

A hardworking and dedicated individual, determined on the road to success, ever ready to take on challenges and accomplish what I set out to achieve.

Newsletter

Search

Archives

  • December 2023
  • April 2023
  • March 2023
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • October 2021
  • September 2021
  • May 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • May 2019

Recent Posts

  • Exploring Flutter Navigation: From Basics to Advanced Routes
  • Web UI Test Automation with Pytest-BDD
  • How to fix IOS compass calibration issues
  • Testing Android Applications With Perfect Coverage
  • How to use useRef hook efficiently? – React

Tags

  • android
  • angular-state-management
  • Automation
  • Compass
  • cross-platform
  • css
  • development
  • firebase
  • hooks
  • ios
  • learn-ngrx
  • ngrx-beginner
  • ngrx/store
  • QA
  • react-native
  • reactjs
  • scss
  • stylesheet
  • styling
  • Testing
  • Test Script
  • UI-UX

Newsletter

Newsletter

Post navigation

Previous I have an idea for an app that hasn’t been made – what do I do?
Next ReactJS Vs React Native: What’s the difference and how to make the right choice?
Schedule an Appointment with our Mobile App Development Expert
Footer Menu
  • Company
    • About Us
    • Portfolio
    • Blog
    • Careers
    • Contact Us
  • Solutions
    • Apps Discovery Services
    • Team Augmentation
    • Enterprise App Development
    • AR/VR Application Development
    • IoT Application Development
    • Wearables Apps Development
    • Field Sales
    • On-Demand Apps Development
  • Technologies
    • iOS
    • Android
    • React Native
    • Flutter
    • Ionic
    • Xamarin
    • NativeScript
    • HTML5
    • Sencha
  • Industries
    • Retail
    • Agriculture
    • Healthcare
    • Pharmaceutical
    • Manufacturing
    • Automotive
    • Logistics
    • Education

US Office

Belmont, California – 1301 Shoreway Road, Suite 160, Belmont, CA 94002

Pleasanton, California – 6701 Koll Center Parkway, #250 Pleasanton, CA 94566

Tel: +1 408 365 4638
Support: +1 (408) 512 1812

Mexico Office

Amado Nervo #2200, Edificio Esfera 1 piso 4, Col. Jardines del Sol, CP. 45050, Zapopan, Jalisco, Mexico

Bulgaria Office

49 Bacho Kiro Street, Sofia, 1000, Bulgaria

Canada Office​

895 Don Mills Road, Two Morneau Shepell Centre, Suite 900, Toronto, Ontario, M3C 1W3, Canada

UK Office

Export House, Cawsey Way, Woking Surrey, GU21 6QX

Tel: +44 (0) 14 8361 6611

UAE Office

Dubai, UAE – Dubai Internet City, 1st Floor, Building Number 12, Premises ED 29, Dubai, UAE

Tel: +971-55-6540154
Tel: +971-04-2505173

Pakistan Office

Folio3 Tower, Plot 26, Block B, SMCH Society, Main Shahrah-e-Faisal, Karachi.

First Floor, Blue Mall 8-R, MM Alam Road Gulberg III, Lahore.

Tel: +92-21-3432 3721-4 

© 2025, Folio3 Software Inc., All rights reserved.

  • Privacy policy and terms of use
  • Cookie Policy
Follow us on
Facebook-f Linkedin-in Instagram

Get a free app audit

[contact-form-7 id="3548" title="Float Banner Form"]