3

Facebook has said that React Native builds on the principle of learn once, write everywhere, and that its goal is not to write cross-platform code.

NativeScript, which is pretty similar to ReactNative in the sense they both use Node.js, do offer the ability to share the same code over several platforms by writing platform specific code in files that uses native components that cannot be shared. It does that by simple naming convention, e.g foo.ios.js.

Since Facebook has not yet released their code for React Native for other platforms than iOS. Is this something they will likely support in the future?

5
  • "in the sense they both use Node.js" - React Native doesn't use NodeJS Commented Jun 28, 2015 at 13:18
  • @BenjaminGruenbaum: Well they list it under requirements, so it appears they do in fact use Node.js. Commented Jun 28, 2015 at 13:36
  • 1
    Node.js is used for development, not as the actual engine. React Native uses JavaScriptCore and NativeScript uses JavaScriptCore for iOS and V8 for Android. Commented Jul 1, 2015 at 13:01
  • 1
    Please see this post on how to share code across platforms in React Native: stackoverflow.com/questions/38506602/… Commented Jul 23, 2016 at 2:15
  • Have a look at Code sharing between React and React Native applications Commented Sep 11, 2017 at 22:07

1 Answer 1

3

I think there are far too many differences between Android, iOS and Web that it makes little sense to build common codebase for the whole projects. Android version will have not only native components, but also the logic of application screens and navigation will be different (iOS/Android/Web have all pretty different navigation patterns). Also there are many native components as part of the iOS/Android projects and they impact how application is started/debugged etc. so probably it is much better to keep Android and IOS parts as separate projects.

How I understand (and sympathise with) Facebok model:

Whenever there is a common functionality (in javascript) that you can separate out without dependency to react-native views, they will do it as separate component that can be reused across web/iOS/Android. For example they have relay library built for web Facebook. It abstracts away access to open-graph data on Facebook servers - the exact same relay library can be used in either environment as it has no dependencies to views, application logic and react as such.

I very much concur with that approach - the best way to do cross-platform is to follow the same practice:

  1. make something work in one of the environments
  2. make it separate, single-purpose library without dependencies to react, views, navigation logic.
  3. use the library elsewhere

So the Android and iOS and Web react-based application for the same "project" will always be three different codebases, but they might have quite a lot of shared javascript code in form of reused libraries.

Sign up to request clarification or add additional context in comments.

7 Comments

Have you checked out NativeScript?
It's more "philosophical" statement rather than particular tool. We developed a lot of apps for different platforms and while some internals are pretty much common across platforms, the UI, navigation patterns are quite different. They are different enough that it makes little sense to make single project to generate iOS and Android. Main part of the application - i.e. what the user sees will have to be anyhow different. So I very strongly believe that reusing code in form of libraries is the best you can do for cross-platform app. We've done that in C before and worked great.
Agree with what Jarek is saying -- a common codebase for whole projects across multiple platforms seems to be the antithesis of what React Native was made for. There are many frameworks that let you write once and run anywhere, and it leads to a really poor user experience. I've installed iOS apps with Android share icons, Android apps with iOS design language, and it's just a poor experience. Just because you can do something doesn't mean you should.
I believe this is how the Xamarin solution works too: varying UI but common logic.
If react native doesn't support cross platform well, why should I use it instead of native development such as Java, Swift in the first place?
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.