Understanding code push for react-native - reactjs

I'm starting building a react native application for both iOS and Android. I've read through the documentation of react native application, but there is one thing now I do not understand:
Is react-native app fully native application?
They say in react-native, that it would be. Then why am I asking this question? Well, I found the reason to ask the question through https://github.com/Microsoft/react-native-code-push. According to code push, they are able to dynamically update the application. And this is done by updating the javascript part of the application. But wait, if the application is real native app, then in the final version of the app there shouldn't be any javascript, am I right? So how are they doing it? Are they compiling the application at code push servers, and then sending the compiled app to the user or what? This really worries me that the react-native app is really not fully native app.
The CodePush plugin helps get product improvements in front of your end users instantly, by keeping your JavaScript and images synchronized with updates you release to the CodePush server. This way, your app gets the benefits of an offline mobile experience, as well as the "web-like" agility of side-loading updates as soon as they are available. It's a win-win!
Also, the reason I'm doubting this is because they say also in code-push documentation, that they are unable to update the native part of the react-native application:
Note: Any product changes which touch native code (e.g. modifying your AppDelegate.m/MainActivity.java file, adding a new plugin) cannot be distributed via CodePush, and therefore, must be updated via the appropriate store(s).
If the react-native app would be fully native, then in the end, all code should be native code. So I don't really understand why would it be harder to update the native native code, than the code that has been compiled to native?

If you change only code in /src/ folder (JS code) and without adding native modules
you can use CodePush to update your application.
If you change code in /src/ folder (JS code) and add some natives modules, you can't use CodePush to update your application.
You're JS code is compiled and read by native components. That's why you can update the JS layer.
If you take a look into your .ipa file and open main.jsbundle (unminify required) you can see your JS code. example:

Related

Correct way of using react with electronjs

I have external api hosted in the cloud and I would like to write desktop app for management. Last version of electronjs I worked with was version 8 and over that time a lot of changed especially from security perspective. The concept of preload was introduced and I would like to know is it fine to write renderer as react app with redux toolkit and make api calls that way or should I use preload script to get data on the server side.
I would like to know what is a proper way of writing such app.
Yes, it is totally fine to use react and redux toolkit in your renderer.
To quickly get started, you could use something like electron react boilerplate or one of the several other boilerplates available online.
I don't understand why you would want to use preload to fetch from the server.
You can treat the renderer as just another browser instance and make requests to the server directly from your react app using fetch or xhr.
Preload is generally used to run code before the renderer has loaded. I generally use it as a bypass to turning on nodejsintegrations for my electron apps, but you can read more about it in the official docs

Difference between Expo CLI and React Native CLI [duplicate]

From the Expo website
Expo lets web developers build truly native apps that work across both iOS and Android by writing them once in just JavaScript.
Isn't this what React Native does? What's the difference?
When you write code in Expo you are writing React Native code. Expo has
two main pieces:
Expo CLI (expo-cli): a developer tool for creating projects, viewing logs, opening on your device, publishing, etc.
Expo client: an app on your phone that lets you open your projects
while you're working on them, without needing to go through XCode or Android Studio, and also lets other people view them too! And if you publish it through expo-cli, people can access it at any time through the Expo client on Android or on iOS if signed in to the same account it was published with. Lastly, we also make it possible to build standalone apps so people don't have to use the Expo client to open it, and you can distribute to the app store and play store if you like.
So Expo is a set of tools built on top of React Native. These tools depend on one key belief held at Expo: it's possible to build most apps without ever needing to write native code, provided that you have a comprehensive set of APIs exposed to JavaScript.
This is important because with React Native you can always drop down to native code. This is incredibly helpful sometimes but it comes at a cost: you need to send people your binaries if you want them to test them, someone on the other side of the world can't just tap a link to open it while you're working on it and you can't just publish it with one click for someone to access it similar to how you would in a browser.
With Expo, we suggest that try to avoid dropping down to native code, if you can. As I mentioned above, we think that with a comprehensive set of APIs available to JavaScript, this shouldn't be necessary for most apps. So, we aim to provide this comprehensive set of APIs, and then build all of the awesome tooling that can exist in a world where the native runtime is shared.
However, if you must find that you want to drop down to native, you can use eject and continue using the native APIs that Expo gives you while having the same level of control as you would in any native project.
Read more about ejecting
A summary of the documentation and answers from Expo employees:
React Native init:
Advantages
You can add native modules written in Java or Objective-C (probably the only but the strongest one)
Disadvantages
Needs Android Studio and Xcode to run the projects
You can't develop for iOS without having a Mac
Device has to be connected via USB to use it for testing
Fonts need to be imported manually in Xcode
If you want to share the app you need to send the whole .apk / .ipa file
Does not provide JavaScript APIs out of the box, e.g., Push-Notifications, Asset Manager, they need to be manually installed and linked with npm for example
Setting up a working project properly (including device configuration) is rather complicated and can take time
Expo
Advantages
Setting up a project is easy and can be done in minutes
You (and other people) can open the project while you're working on it
Sharing the app is easy (via a QR code or link), you don't have to send the whole .apk or .ipa file
No build necessary to run the app
Integrates some basic libraries in a standard project (Push Notifications, Asset Manager, etc.)
You can eject it to ExpoKit and integrate native code continuing using some of the Expo features, but not all of them
Expo can build .apk and .ipa files (distribution to stores possible with Expo)
Disadvantages
You can't add native modules (probably a game changer for some)
You can't use libraries that use native code in Objective-C/Java
The standard Hello World app is about 25MB big (because of the integrated libraries)
If you want to use: FaceDetector, ARKit, or Payments you need to eject it to ExpoKit
Ejecting it to ExpoKit has a trade-off of features of Expo, e.g. you cannot share via a QR code
When ejecting to ExpoKit you are limited to the react-native version that is supported by ExpoKit then
Debugging in ExpoKit (with native modules) is a lot more complicated, since it mixes two languages and different libraries (no official Expo support any more)
Sources:
Detaching Expo Apps to ExpoKit: Concepts
Difference between react-native-init and create-react-native-app #516
Ejecting to ExpoKit
The answer of Brent Vatne is fine, but I'd like to add some details.
Expo expands the API surface of React Native
React Native does not give you all the JavaScript APIs you need out of the box, but only most primitive features. React Native developers are expected to use Android Studio or Xcode to link additional native libraries.
Expo aims to enhance React Native and provide all the JavaScript APIs you need for the most common needs. It is basically a set of well-defined quality native libs already packaged for you in a single lib: ExpoKit. Sometimes these libraries are actually already existing in the React Native world, and integrated into ExpoKit.
It is also important to notice that the Expo team can't include every library out there into ExpoKit (which may create some frustration) because the Hello, World! app size would grow, as it would ship a lot of API's that wouldn't be used in most apps.
Expo provides over-the-air JavaScript updates
Like other systems (CodePush, etc.), Expo provides a system to update your app over the air. This means you upload your JavaScript bundle on a CDN and the mobile apps will automatically download and use the new JavaScript code on next startup (without requesting a publication/review from the stores).
Expo provides a CLI tool to upload/manage the JavaScript bundles on their CDN. For development you can also choose to become the CDN and host the JavaScript bundle on localhost. And Expo XDE is just a visual wrapper on the CLI.
Expo provides a generic client
The Expo client is a generic client that permits to load any app that is compatible with Expo. All the Expo apps do share the exact same native runtime (React Native + ExpoKit). The only difference is the JavaScript code that we give them. The Expo apps you publish to the app stores have your JavaScript bundle URL hardcoded in it. The Expo client is build in a specific way so that you can choose from which URL to load the JS, by scanning a QR code or providing a URL.
Note that this client can also load JavaScript bundles from localhost, and make your development experience easier: There isn't any need for Xcode or Android Studio, and it gets much faster to get your first Hello, World! app running on the phone (from hours to minutes). You can actually develop on an iPhone without a Mac and the setup takes two minutes.
Actually, as the Expo SDK can be upgraded, the Expo client includes a compatibility layer so that it is able to run the last five SDK versions.
Expo provides a build service
As all the Expo apps share the same native code, Expo is able to easily build these apps for you. They have created a cloud build service.
The major thing that differentiate two apps built by Expo is just the hardcoded URL to which the app is supposed to download the JavaScript bundle to run.
Expo does some other things for you, like providing a declarative way to setup app icons, orientations, permissions, API keys, helping you setup push notifications, provisioning profiles... many settings have to be hardcoded at build time in the app and can't be changed over the air.
Expo is to React Native what PhoneGap is to Cordova
React Native is similar Cordova. It is not the same view technology (native vs webview), but both allow you to control native features from JavaScript, and both offer a plugin system so that developers can easily add new JavaScript and native bindings.
PhoneGap is similar to Expo. They both try to enrich the raw API of the underlying platform they are built on with a predefined set of additional native plugins. PhoneGap also offers a build service and has a generic client that work as long as you use the approved plugins.
Conclusion
As you can see, Expo is a set of tools. At the end, it permits to easily develop, share and publish to the stores your mobile projects. It is quite similar to the PhoneGap experience (but a lot better and less confusing).
I will definitively recommend Expo for any new React Native greenfield project, except these two cases:
You already know you need API's that are not available in Expo and won't be anytime soon
You care a lot about your app size (Hello, World! is more than 25 MB due to the large size of ExpoKit, but after that it does not increase much as it is just JavaScript)
It is explained in the official Expo documentation
What is the difference between Expo and React Native?
Expo is kind of like Rails for React Native. Lots of things are set up for you, so it’s quicker to get started and on the right path.
With Expo, you don’t need Xcode or Android Studio. You just write JavaScript using whatever text editor you are comfortable with (Atom, vim, emacs, Sublime, VS Code, whatever you like). You can run XDE (our desktop software) on Mac, Windows, and Linux.
Here are some of the things Expo gives you out of the box that work right away:
Support for iOS and Android
You can use apps written in Expo on both iOS and Android right out of the box. You don’t need to go through a separate build process for each one. Just open any Expo app in the Expo Client app from the App Store on either iOS or Android (or in a simulator or emulator on your computer).
Push Notifications
Push notifications work right out of the box across both iOS and Android, using a single, unified API. You don’t have to set up APNS and GCM/FCM or configure ZeroPush or anything like that. We think we’ve made this as easy as it can be right now.
Facebook Login
This can take a long time to get set up properly yourself, but you should be able to get it working in 10 minutes or less on Expo.
Instant Updating
All Expo apps can be updated in seconds by just clicking Publish in XDE. You don’t have to set anything up; it just works this way. If you aren’t using Expo, you’d either use Microsoft Code Push or roll your own solution for this problem
Asset Management
Images, videos, fonts, etc. are all distributed dynamically over the Internet with Expo. This means they work with instant updating and can be changed on the fly. The asset management system built-in to Expo takes care of uploading all the assets in your repo to a CDN so they’ll load quickly for anyone.
Without Expo, the normal thing to do is to bundle your assets into your app which means you can’t change them. Or you’d have to manage putting your assets on a CDN or similar yourself.
Easier Updating To New React Native Releases
We do new releases of Expo every few weeks. You can stay on an old version of React Native if you like, or upgrade to a new one, without worrying about rebuilding your app binary. You can worry about upgrading the JavaScript on your own time.
But no native modules…
The most limiting thing about Expo is that you can’t add in your own native modules without detaching and using ExpoKit.
Expo CLI
Pros:
No need to install Android Studio or Xcode to start building.
Not a lot of configuration required.
Mobile UI to easily view on both Android and iPhone using barcode scanning. Also easier to view with online iPhone and Android simulators.
Faster setup and development.
Cons:
Native dependencies cannot be added.
Creating .apk and .ipa files is difficult.
Size of apk/ipa is huge.
React Native CLI
Pros:
Easily add native dependencies.
Creating .apk and .ipa files is much easier than with Expo
Note: React Native CLI is the framework to use to work directly with the React Native framework.
Cons:
High level of configuration required.
Basic knowledge of Android and iOS folder structure required.
As you can see, both approaches have their pros and cons; you should always decide which to use based on your needs for the project.
The answers about advantages and disadvantages of Expo and React Native CLI are complete. I want to mention another point as my personal experience.
Expo contains many modules by default in the project and it makes easier to work with it. But it has a big problem in the production phase, because the built Android and iOS versions have such a great size. For example, if you have a single page with a 'Hello, World!', the APK file size would be about 19 MB. Having a same project in React Native CLI will result in an app with the size of 6 MB.
So personally I do not recommend using Expo if you want to develop a commercial app.
I would make a note here that Expo is using an older version of React, 16.5, which would prohibit you from using the new hooks feature.
If you decide to go with Expo, be mindful to watch your version controls. Older versions of, say, react-navigation will need to be used with the 16.5 version if you are getting weird errors.
Expo is a wrapper of React Native CLI, which does things under the hood for the ease of developers. The most common problem that Expo solves is that the developer doesn't have to worry about the linking issues of the libraries that often occur in React Native CLI. Expo comes up with tons of libraries now that are extremely helpful for robust development. One of the benefits of Expo is you can develop an app for Android and iOS without having a Mac through Expo mobile application.
As it has tons of benefits, there are some cons of using Expo:
App size increases like hell. A simple Hello, World! app size is more than 30 MB
At times when you need to implement some native functionality into your app, you will need to eject from Expo and go back to React Native CLI. And you can never revert it back to Expo CLI.
I've experienced more than a year that works with Expo.
If the size of the app is not important for you, use Expo because it's easy to implement Map and Push easier than React-native, but at the end of the project, if you want to publish app in Google Play or another store, you have a challenge removing some permissions in your APK.
In React Native you can change everything, but for importing some library, such as push notification or the map, it needs some challenges too, because you have to add these libraries manually to Android and iOS projects.
A summary of the documentation and answers from Expo employees:
React Native init:
Advantages
You can add native modules written in Java or Objective-C (probably the only but the strongest one)
Disadvantages
Needs Android Studio and Xcode to run the projects
You can't develop for iOS without having a Mac
Device has to be connected via USB to use it for testing
Fonts need to be imported manually in Xcode
If you want to share the app you need to send the whole .apk / .ipa file
Does not provide JavaScript APIs out of the box, e.g., Push-Notifications, Asset Manager, they need to be manually installed and linked with npm for example
Setting up a working project properly (including device configuration) is rather complicated and can take time
Expo
Advantages
Setting up a project is easy and can be done in minutes
You (and other people) can open the project while you're working on it
Sharing the app is easy (via a QR code or link), you don't have to send the whole .apk or .ipa file
No build necessary to run the app
Integrates some basic libraries in a standard project (Push Notifications, Asset Manager, etc.)
You can eject it to ExpoKit and integrate native code continuing using some of the Expo features, but not all of them
Expo can build .apk and .ipa files (distribution to stores possible with Expo)
Disadvantages
You can't add native modules (probably a game changer for some)
You can't use libraries that use native code in Objective-C/Java
The standard Hello World app is about 25MB big (because of the integrated libraries)
If you want to use: FaceDetector, ARKit, or Payments you need to eject it to ExpoKit
Ejecting it to ExpoKit has a trade-off of features of Expo, e.g. you cannot share via a QR code
When ejecting to ExpoKit you are limited to the react-native version that is supported by ExpoKit then
Debugging in ExpoKit (with native modules) is a lot more complicated, since it mixes two languages and different libraries (no official Expo support any more)
Expo and React Native are two popular tools for building mobile apps, but they have some differences. Expo is a free and open-source platform that allows developers to quickly build apps with JavaScript and React Native. It provides a set of tools, libraries, and services that make it easier to get started with building a mobile app. React Native, on the other hand, is a framework for building mobile apps using React. React Native provides a more direct way to access native APIs and components, and allows for more customization and control over the app development process. In summary, Expo provides a faster and easier way to get started with building a mobile app, while React Native provides more control and customization.
Another difference between Expo and React Native is the deployment process. With Expo, you can publish your app directly to the app stores with just a few clicks. You can also take advantage of other services that Expo provides, such as push notifications and authentication. On the other hand, when building a React Native app, you'll need to use tools like Xcode or Android Studio to compile and package the app for deployment. Additionally, you'll have to handle the distribution process yourself, which can be time-consuming and complex.
In terms of performance, React Native generally has better performance compared to Expo as it has a direct access to native APIs and components, whereas Expo relies on its SDK. However, the performance difference may not be significant in many cases.
In conclusion, both Expo and React Native have their own pros and cons, and the choice between them depends on the specific requirements and goals of your project. If you're just starting out with mobile app development and want a fast and easy way to get started, Expo is a great option. However, if you want more control and customization, React Native might be the better choice.

Can React be added to an existing .NET Core project?

I am wondering how to add React into an existing ASP.NET Core MVC project in Visual Studio 2022 (no, I cannot use VS Code). I can see templates in VS2022 to create new React projects, but I have been unable to find any tutorials, videos, blogs or vlogs to show how to add React into an existing project, so I am wondering if it can be done.
I have added react.js and react-dom.js to the project and I am able to create pure javascript components using those (no JSX), but I would like to utilize more of what React has to offer.
=========================================
After the request to ask a more specific question, including the project in question . . .
I am currently on a small team working on an ASP.NET Core 6.0 MVC application. It is currently using jQuery and jQuery-UI components within the CSHTML views for the UI. There are some $.ajax calls to an API for data, but most data is loaded with the model of the view.
My boss has asked me to look into utilizing React and the reusable components on the next module that we will be working on, without replacing the entire app into a "new React application".
Every single example, video, tutorial that I have watched uses VS Code or some other IDE (other than Visual Studio) to run commands on the CLI to "Create New React App", but nothing on how to "Add React to an Existing App".
Before I go back to him and tell him that I do not think we will be able to do what he is asking, I thought I would see if anyone here could point me to anything that could show that it could be done.
Specific question: can React be added to an existing ASP.NET Core 6 MVC app in Visual Studio Professional 2022 in a way that will allow linting, testing and JSX for reusable React components?
I think I know what you are trying to achieve:
Replace what you used to do in jQuery with React and to do so without
having to move to the whole VSCode, Create-React-App (or Vite), "npm
run stuff" environment.
You can indeed do so: I have setup a .Net6 project which is the standard MVC stuff that you are already used to, stripped out jQuery that comes with it, added Bootstrap 5 (no jQuery) and then used this (https://beta.reactjs.org/learn/add-react-to-a-website) to actually add React just as a library - JSX included.
The final bit that is missing is the JSX babel warning I get. I just need to add something to my MSBuild that will minimise all my JavaScript and JSX components and I'm done.
Everything in Visual Studio too.
You can easily build components and I use a watcher on session storage to tell the state to render and it works pretty well.
The main reason I did it was so I didn't have to use NextJS because I don't see the point in learning/using NextJS and it's whole environment because its "Server Side Rendered" when that is what we have been doing for years in .Net MVC, Wordpress, and every framework/CMS you can think of.
Note, I am not against Next I am just at a point where I only use/learn tech if there is a benefit. I can clearly see the benefit of using React over jQuery.
My public facing app needs to be actual HTML coming from the server not a client side rendered page.
My private login area I build using Create-React-App (or maybe I'll switch to Vite - as I am rebuilding it) and I do all that in VSCode.

Can you use Next.js with a React Native App?

I have recently been learning Next.js for React and I like the routing system a lot more than react navigation. Now I am about to start a react native frontend and I was wondering if you can utilise Next.js's routing system in react native as well as it's other benefits. I know that you can use it with expo but I am a little confused as to how it all works and I am starting to wonder if the Next only works on the web side of things..
Does anyone have any experience with this? Any information would be much appreciated.
Thanks!
Unfortunately you can't do such thing, due to the fact that the views of react native are Equivalent to native views in Java or Objective C. When you get build version of react native , you are receiving Java or Objective C native views thank to react native's RN bridge. Meanwhile, when you get build version of your Next.js project, you are going to receive HTML, CSS, JS and these codes are not familiar to native platforms.
However, you can use your Next.js backend as your endpoints and fetch data in react native via calling this endpoints.
UPDATE Oct_2022:
there seem to be a new tool created for this purpose , called solito. I haven't used it personally but it might meet your needs.
UPDATE Jan_2023:
It looks like there are more Tools being created for Both web and native platforms , check react-native-web
You can check out Tauri. With that you can create from Next.js, React, Svelte etc. cross-platform native Apps. Currently, Windows, Mac and Linux are supported stable. Android & iOS is currently in Alpha
The magic behind Tauri is, that they provide around your Frontend library/framework a Rust layer with that you can interact with platform native APIs. They call it Commands.
You can find the guide for Next.js here.
Also, if it's not exactly answering the question - "Is it possible to use Next.js in React native?" - I hope that it's helping everyone who is searching for a solution to use Next.js as Cross-Platform Frontend Framework
I read a thread on ycombinator on this where Solito was mentioned and tried it out. In the comments I found this, which suited my needs best :
https://github.com/mlynch/nextjs-tailwind-ionic-capacitor-starter
Solito looks nice but the all in one repo was using Expo, which I did not like because it installs an extra app. I would check it out for yourself and look at the repo above too, which is using Capacitor and is aimed at having one codebase for the two apps.
Both examples worked well. For Tauri I could not find the mobile support so I didn't try it. I would be interested to read more about people's experience with running a minimum stack for cross platform development.
Note : Solito uses React Native, the repo uses Capacitor instead, which may also suite your needs because it works on both iOS and Android.
There's a lot of support now for this tech stack. Besides solito, which is already mentioned, there is also https://tamagui.dev/ which tries to solve all the issues with react-native-web and Next.js along with Solito.
While they currently recommend a monorepo (which adds a lot of complexity in my opinion), I'm playing around with a vanilla React Native + Next.js here: https://github.com/criszz77/luna
This command should get you started to play around:
npx react-native init MyAppName --template #criszz77/luna

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

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!!!
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
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.
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
You could check out ReactXP (https://microsoft.github.io/reactxp/) which uses reactjs and react native to build cross platform apps.
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
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.
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
Two popular options addressing code share across web, iOS, Android & Windows are
React Native Web - https://github.com/necolas/react-native-web
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.
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.
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.
Yes we can reuse the code. And it works really well.
Here is a sample application repository
https://github.com/spoman007/reactXstarter
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

Resources