Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we share code between react webapp and react native app and is react-native production ready

Tags:

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!!!

like image 720
bhb Avatar asked May 09 '17 12:05

bhb


People also ask

Can I share code between react and React Native?

React Native for Web is an open source project that enables you to use React Native core components in React web applications. React Native for Web uses React DOM to render JavaScript code that is compatible with React Native in a web browser, making code sharing possible.

Can React Native run on web and app?

Can React Native be used for web and mobile? Yes! With React Native for Web, developers can write a single React Native application that can run natively on Android and iOS, as well as on a web browser using standard web technologies.

Is it possible to use native code alongside React Native?

However, through native modules, you can write native code that communicates with native calendar APIs. Then you can invoke that native code through JavaScript in your React Native application. In the following sections you will create such a Calendar native module for both Android and iOS.

Is coding in react the same as React Native?

While Reactjs is basically a JavaScript library and React Native is the entire framework, the former is the heart of the latter, and compliments each other. If Reactjs is optimal for creating apps with high functionality and complex calculations, then React Native is ideal to give a native feeling to your mobile apps.


2 Answers

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

like image 43
Rakesh Avatar answered Oct 08 '22 02:10

Rakesh


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

like image 73
Viktor Sec Avatar answered Oct 08 '22 02:10

Viktor Sec