Is it possible to setup a project which has code for both React Native(Mobile app) + React(web), having the code shred between platforms except for the UI part.
Have done something similar with Angular + NativeScript using this seed, which enables code sharing between native app and web application(Except for the UI layer). Looking for something similar for React + React Native.
Please share if you know any such seed for React Native + Angular as well, if available.
Yes, absolutely possible. We've done it before using this lib
react-native-web
. https://github.com/necolas/react-native-webbeside
index.ios.js
andindex.android.js
, you will need createindex.web.js
, the content should be similar like this.also you need to create your own nodejs code to serve up the bundle. full reference
You can create 3 stand-alone applications - React, React-native & Server. Both React & React-native will use the same services from your back-end app. Else go with a single app where on loading home page, app will understand the device and render React / React-native code as per the device.
I do it this way.
1) Create a React Native project.
2) Add
react-dom
andreact-scripts
dependencies topackage.json
and install them.3) All the component code is separated this way:
My regular React Native component:
Changed for using in web:
And then I use in it in all the platforms:
For this to work nicely with an old project I had to implement some dummies, but it all can be done better by your own layer of abstraction. Part of my example:
But, as I said, this was necessary only because I did not have much time and had not known in advance that I would have to port to web.
You can give a try to React-Native-Web, but imho you should create 2 different projects, isolate and copy what can be used on both (like api requests and util functions). Your code will be easier to debug and maintain.
You can try the repo that I tried to prepare:
https://mehmetkaplan.github.io/react-spa-jwt-authentication-boilerplate/
This has a step by step guideline that enables to share common logic between react and react-native applications.
It aims to differentiate only in the presentation layer. Other than that all logic is compiled to be shared between applications.
It also comes with facebook and google logins, database (mysql) integration, WebView task generation, etc.
And also it gives the fundamental know-how on "single page applications", "JWT (json web token) based security", etc..
Once read the README, you can simply clone the repo and set your environment (Database) and start developing business logic on top of the shared code structure and security baseline.
Jonathan Kaufman has a good article on how to set this up: http://jkaufman.io/react-web-native-codesharing/
The basic strategy is to have a different entry point (index.js) for each platform (android/ios/web). Then the majority of your non-rendering code can live in a shared
app
orcommon
folder. You'll still need to segregate your rendering code (i.e. uses ofView
,div
, etc.), though, as that will differ by platform.Pay attention to the comments on that article as well, as there's some good discussion on the pitfalls of this approach. Example: