25

We have a stable version of a widget developed with reactjs. We would like to develop mobile version of the same. Is it better to develop with react native and share the code across the 2 apps or is it better we develop the widget natively.

Bare in mind that we do have expertise in both(react and android dev) but we do not want to invest to much time on developing the entire app again.

Are there any tools/resources available to get this done faster if we choose react-native?

Resources available online:

http://jkaufman.io/react-web-native-codesharing/

https://arielelkin.github.io/articles/why-im-not-a-react-native-developer.html

https://medium.com/@felipecsl/thoughts-on-react-native-from-an-android-engineers-perspective-ea2bea5aa078

Cheers!!!

1

11 Answers 11

22

Instagram, Tesla, AirBnB, Discord, Bloomberg all have production apps written in React Native. I'll let you decide for yourself whether it's production-ready in your opinion.

While React Native enables you a significant code reuse between iOS and Android (we are currently building an app for client which - appart from external libraries - reuses over 99% code between iOS and Android), it is not designed to share code with React webapps.

The Facebook's philosophy with React Native, instead of write once, run anywhere is rather learn once, write anywhere.

You can definitely use your designs and architecture, but you would need to rewrite most of the code. It might still be more efficient than developing two separate Java and Obj-C/Swift apps, though.

Update: In 2018 Airbnb decided to sunset React Native in their production apps. They wrote an insightful article about their experience and reasoning. It is very relevant for anyone thinking about using React Native. https://medium.com/airbnb-engineering/react-native-at-airbnb-f95aa460be1c

1
  • 2
    In a pinch though, React Native can be ported to the web. My company has done this when a client suddenly wanted a website with similar functionality as their native app. It only took a couple of developers a week or two to port a year-long project back to the web from React Native. Not really ideal as far as web design goes, but it worked in an "emergency". Commented May 9, 2017 at 13:51
16

You cannot just use your whole code into the react-native application. First and foremost, you have to follow the react-native architecture and then develop your UI using react-native components.

You’ll certainly have to write your components separately for a mobile app and web app. But you can always reuse the business logic, API Communication layer.

Create the reusable component and share it in the Shared folder and reuse Mobile/Web anywhere.

Convert react to the react-native platform is an easy process. A step by step process how to reuse code in react native explain in the image with react native web view as an introduced bride as react-hooks

Step by step guide how to do React vs React Native reusability & what percentage of reusability that we can achieve between react and react native code will explain in the below part.

Sharable code between React & React Native:

  • Business Logc
  • Communication with API
  • Stores, Reducers, Actions and Services
  • Helpers, Constants, Storage Services
  • HOCs (Higher-Order Components)
  • Mobile / Web specific:

Dedicated code (specific code) for Mobile & Web separately

  • Presentational components
  • Navigation / routing
  • Styles

Setting up a shared project

Make sure you are at the project root folder

$ mkdir -p packages/components/src packages/mobile packages/web

Create react native project using react-native-cli inside 'packages/mobile`

Create react app using create-react-app inside packages/web

Create package.json at the root directory to enable Yarn Workspaces

Create a shared folder

Now create a common or shared folder where the common code of react and react native will exist.

$ mkdir -p packages/common

Create package.json file in common folder

Name the package and add main(entry file)

Configure React Web application

Add react-app-rewire-yarn-workspaces and react-app-rewired in dev dependencies in your web/package.json

Change your scripts from react-scripts to react-app-rewired

"start": "react-app-rewired start"

"build": "react-app-rewired build"

"test": "react-app-rewired test --env=jsdom"

"eject": "react-app-rewired eject"

Add config-overrides.js inside web

Configure React Native Mobile application

Configuring react-native on mono repo is a little bit tricky part. We need to understand two things before making workspaces work in our react native app.

  • Symlinking
  • No Hoist

Symlinking

symlink is a term for any file that contains a reference to another file or packages. To achieve symlinking we will use wml.

And finally

Create the reusable component and share in the Shared folder and reuse Mobile/Web anywhere.

Migrating from Web (React) to Mobile(React Native) or Mobile (React Native) to the Web (React)

Depends on the following key points

  • Followed Coding Guideline Standard
  • Modularised development
  • Component-based development
  • Segregation of sharable code
  • and code design for both (Web or Mobile) or not like the example Image

Here I tried for a simple explanation about how to share code between React and react-native with react-native-web. I'll add a detailed process (step by step) guide for convert react native app to web as well here in the coming days.

Code difference to migrate from ReactJS to ReactNative

Migration from React to React Native

Conclusion - based on my understanding and depends on the above points, I can say that you can reuse from 20-50% of React JS code to React Native platform

From Scratch Development

Conclusion - based on my understanding and depends on the above points, I can say that you can reuse from 50-70% of React JS code to React Native platform

References website - Codingular

References website - Jkaufman

2
  • I have an electron app that uses ReactJS as its renderer and I have an Expo app that uses React Native. How can I share code between the two? Commented Jun 25, 2022 at 3:14
  • Create Project Directory : ' mkdir my-app' ' cd my-app' Create React Web inside my-app : npx create-react-app webApp Create React Native inside my-app : npx react-native init mobileApp Create Common Directory inside my-app : mkdir common cd common npm init Run Web & Mobile : cd webApp npm start cd mobileApp yarn run ios
    – Rakesh
    Commented Jul 10, 2023 at 13:25
6

You could check out ReactXP (https://microsoft.github.io/reactxp/) which uses reactjs and react native to build cross platform apps.

4

Rather than bloat this answer too much, I wrote a fairly in-depth guide about this at https://dev.to/kylessg/a-sensible-approach-to-cross-platform-development-with-react-and-react-native-57pk.

To summarise though, you can get great amounts of codesharing between React and React Native but don't overreach on what you're sharing. Doing so may leave your code harder to maintain where you're trying to oversimplify valid differences between mobile and web.

For myself, this is along the lines of:

Shared:

  • Business logic
  • Communication with API
  • Polyfill possible shared functionality into an API (e.g. communication with analytics, local storage, network detection etc)
  • Stores, reducers app actions
  • HOCs

Web/Mobile specific:

  • Presentational components
  • Navigation / routing
  • Styles
2

It can be shared among mobile app and web app. You'll certainly have to write your components again for mobile app. But you can always reuse the business logic.

If you have used React in your web application, then the work becomes even easier. One of the major hazards in React native is State Management. Now the complexity depends upon your application. Usually it is advisable to use Redux along with React Native.

The real question arises is how much code you will be able to share. Hope this helped.

1

Nowadays there is a tool called react-native-web which lets you share code between web and mobile.

Read more: https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/

Repo: https://github.com/necolas/react-native-web

1

Two popular options addressing code share across web, iOS, Android & Windows are

  1. React Native Web - https://github.com/necolas/react-native-web
  2. ReactXP - https://github.com/microsoft/reactxp

Below is an excellent excerpt from https://microsoft.github.io/reactxp/docs/faq.html highlighting the difference.

How does ReactXP differ from React Native for Web?

React Native for Web is an open-sourced library developed by engineers at Twitter. We started implementing ReactXP before React Native for Web was available.

The goals behind these two efforts are similar, but the approaches differ. ReactXP is a layer that sits on top of React Native and React, whereas React Native for Web is a parallel implementation of React Native — a sibling to React Native for iOS and Android.

ReactXP generally exposes only those props, style attributes, and APIs that are available across all platforms. If you write to ReactXP’s abstraction, you can have high confidence that it will run on all supported platforms in the same manner. The same can be achieved with React Native for Web/iOS/Android, but you need to be more careful about which components, props, and APIs you use.

0

You can certainly share the storage and the network layer b/w the web and mobile react code base.
Although, in practice you would be only able to share a part of the storage layer, because you might want to fetch data differently in case of a mobile app and a web app.

0

As of today the philosophy still remains as learn once write everywhere. As stated very correctly in other posts, both react and react-native being JavaScript based, with an architectural design approach you can share your business logic.

On the other hand, because of the nature of the hardwares, it is completely normal to have different presentation layers for web (mostly on your desktop/laptop browsers) and mobile (native).

The question asks for a tool that can speed up the development process. For this purpose this github repo named react-spa-jwt-authentication-boilerplate (*) can be used as an example. It shares the "business logic" between web and native versions via a folder named common-logic by keeping exact copies of *.js files. On the other hand navigation and presentation layer differs. It implements a sample authentication process which itself can also be used as a baseline for new projects.

(*) Disclimer: I am the implementer of the repo. We needed to implement the repo for the exact same need of reducing development time between mobile and web applications.

0

Yes we can reuse the code. And it works really well. Here is a sample application repository https://github.com/spoman007/reactXstarter

1
-2

you can do that by turning stuff like code, hooks, props and even design-tokens into shared components:

Blg post with code examples
Tutorial video on youtube

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.