Skip to main content

Tell us about your project

Foggy pic of red and blue element converging

Sharing code between React JS and React Native is an interesting idea. Writing in one language and
running it both on a Web and as a native App. But because React Native doesn't use HTML to render the
App and provides special components, it can't be done just out of the box.

After hours of research, I`ve found a way to reuse most of the code. In other words, React Native has a platform, a specific way of loading modules. In official documentation under Platform-specific extensions, it's not written that React Native detects .native extensions that are used for both iOS and Android. Knowing that I will use it to my advantage.

Example app

In this article, I will create a simple App that will not have any logic, just a Title and a Lorem Ipsum text that will run both as a Web App and an Android/iOS native App. I will start by generating a bare React Native App with:

  1. $ react-native init ReactCodeReuse
  2. $ cd ReactCodeReuse/

 

Once we create the index.web.js file in ReactCodeReuse/ folder X, that will be the entry point for the Web App ( the public/ directory which will be our web folder and the app/ folder ). The main App code will be in the newly created app/ folder, there we'll normally put all our components, reducers, actions, etc.

The next thing is to install all dependencies. For that I will use the YARN package manager:

  1. $ yarn add react-dom babel-loader babel-core gulp gulp-webserver webpack webpack-stream

 

I won't get into what an individual package is, though, through the article we'll talk about some of the packages and what they are used for.

Now that the dependencies are installed we can proceed with setting up the Web part, so when we begin to code the App components, we'll see them in the browser. We'll start with creating webpack.config.js, a config file for the Webpack package manager, which is an open-source JavaScript module bundler. Webpack takes modules with dependencies and generates static assets representing those modules. In other words, it will take all our code with all its dependencies like the ones we installed and pack them in a single .js file that is going to be used in our Web App as all the logic for displaying, functionality, etc.

  1. module.exports = {
  2.     entry: "./index.web.js",
  3.     output: {
  4.         filename: "public/js/script.js",
  5.         sourceMapFilename: "public/js/script.map"
  6.     },
  7.     devtool: '#source-map',
  8.     module: {
  9.         loaders: [
  10.             {
  11.                 loader: 'babel-loader',
  12.                 exclude: /node_modules/
  13.             },
  14.         ]
  15.     }
  16. };

 

In the code above I have specified what's our App's entry point (index.web.js), where output is to be created (in folder public/js), and also the name of the output .js file (script.js.). We are using babel-loader because this package allows transpiling JavaScript files using Babel and Webpack.

With that done, we must now create gulpfile.js which is a config file for gulp.js. Gulp is a task execution tool based on the Node.js and the Node Package Manager (npm) package management system. It is used to automate tasks that are repetitive and time-consuming.

  1. const gulp = require('gulp');
  2. const webserver = require('gulp-webserver');
  3. const del = require('del');
  4. const webpack = require('webpack-stream');
  5. const webpackConfig = require('./webpack.config.js');
  6.  
  7. gulp.task('clean:build', function() {
  8. del('./public/js/*')
  9. });
  10.  
  11. gulp.task('build', ['clean:build'], function() {
  12.     gulp.src('./index.web.js')
  13.         .pipe(webpack(webpackConfig))
  14.         .on('error', function handleError() {
  15.             this.emit('end'); // Recover from errors
  16.         })
  17.         .pipe(gulp.dest('./'));
  18. });
  19.  
  20. gulp.task('watch', function() {
  21.     gulp.watch(['./app/**/**/**/*', './public/css/**/*', './index.web.js'], ['build']);
  22. });
  23.  
  24. gulp.task('serve', ['watch'], function() {
  25.     gulp.src('public')
  26. pipe(webserver({
  27.             fallback: 'index.html',
  28.             livereload: true,
  29.             directoryListing: false,
  30.             open: true
  31.         }));
  32. });

 

The above code explains that first we loaded all necessary modules, and then we created the tasks that would help us in development.

The task "clean:build" deletes our public/js/ folder, where our script.js ( the “brain” of our Web App ) is residing. The task "build" starts by calling the "clean:build" task, then initiates the building process of our script.js file.
The "watch" task continuously looks if there are any changes in all subfolders of our app folder, changes on index.web.js, and changes in the public/css/ folder. If there are some changes, it calls the "build" task to rebuild the script.js file.

The "serve" task first calls the "watch" task, and starts the Web server in our public/ folder. The Web server's starting point is the Index.html file. 'Livereload' is true, so we don't have to reload the App every time we make changes.

In the public/ folder we need to create the index.html file and a css/ folder with a style.css file within. Index.html is going to be a simple .html file:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.     <head>
  4.         <meta charset="UTF-8">
  5.         <title>React Code Reuse</title>
  6.         <link rel="stylesheet" href="<a href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  7. </a>        <link rel="stylesheet" href="css/styles.css">
  8.     </head>
  9.     <body>
  10.         <div id="root"></div>
  11.         <script src="js/script.js"></script>
  12.     </body>
  13. </html>

 

The important parts of this .html are:

  • the script tag with a call of our script.js which is the bundled App;
  • style.js file which is going to be our stylesheet for the App, and
  • div with id=”root”.

For good measure, we added bootstrap CSS as well.

Now that the configuration is out of the way, we can proceed with App creation. First, we create the folder components/ in the app/ folder. Inside the components/ folder, we create the folder appscreen/. This is where we are going to put our DOM code for Web and native versions. Return to the root folder of the App and populate the index.web.js:

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import App from './app/components/appscreen';
  4. ReactDOM.render(<App />, document.getElementById('root'));

 

Here we`ve created an entry point for our Web App by ReactJS standards. Here we are importing the App component which is our root component of the App, within this element all the other components, functionality, etc are contained and this all will be loaded in the element with id=”root”, which is our div inside public/index.html. We must do the same thing for the native App entry point, but by React Native standard, so in index.js file:

  1. import { AppRegistry } from 'react-native';
  2. import App from './app/components/appscreen';
  3. AppRegistry.registerComponent('ReactCodeReuse', () => App).

 

When in import component like we did index.web.js and in index.js

import App from './app/components/appscreen';

  1. import React from 'react';
  2. import screenView from './screenView';
  3. export default class screenContainer extends React.Component {
  4.     render() {
  5.         return (
  6.             <screenView/>
  7.         )
  8.     }
  9. }

 

In this code snippet, we are calling the screenView file from the same folder as index.js. Previous knowledge that React Native is calling the file with a .native in its name helps us. When that component comes up for rendering, it will be from file screenView.native.js, and when the same component comes up for rendering in Web it will call screenView.js because React JS doesn't recognize .native in the name.

We will now populate screenViews files screenView.native.js

  1. import React, { Component } from 'react';
  2. export default class screen extends React.Component {
  3.   render() {
  4.     return (
  5.         <div className="container">
  6.             <div>
  7.                 <p className="title">header Text</p>
  8.             </div>
  9.             <div>
  10.                 <p>
  11.                     Lorem Ipsum …
  12.                </p>
  13.             </div>
  14.         </div>
  15.     );
  16.   }
  17. }
  18. screenView.native.js
  19. import React, { Component } from 'react';
  20. import { View, Text, StyleSheet } from 'react-native';
  21.  
  22. export default class Screen extends React.Component {
  23.     render() {
  24.         return (
  25.             <View style={styles.container}>
  26.                 <View>
  27.                     <Text style={styles.title}>header Text</Text>
  28.                 </View>
  29.                 <View>
  30.                     <Text>
  31.                         Lorem Ipsum …
  32.                     </Text>
  33.                 </View>
  34.             </View>
  35.         )
  36.     }
  37. }
  38.  
  39. const styles = StyleSheet.create({
  40.     container: {
  41.         flex: 1,
  42.         alignItems: 'center',
  43.         backgroundColor: '#add8e6',
  44.      },
  45.     title: {
  46.         fontSize: 32,
  47.         textAlign: 'center'
  48.         color: '#FFFFFF',
  49.         fontWeight: 'bold',
  50.         marginBottom: 20
  51.     },
  52. })

 

The last thing we must do is to apply those styles to the Web App, so in public/css/style.css add some CSS for styling the view.

Public/css/style.css

  1. body,
  2. html{
  3.     height: 100%;
  4.     width: 100%;
  5. }
  6. #root{
  7.     height: 100%;
  8. }
  9. .container{
  10.     height: 100%;
  11.     vertical-align: middle;
  12.     text-align: center;
  13.     background: #add8e6;
  14. }
  15. .title{
  16.     font-size: 32px;
  17.     text-align: center;
  18.     color: #FFFFFF;
  19.     font-weight: bold;
  20.     margin-bottom: 20px;
  21. }

With coding out of the way the only thing left is to start both Apps and we are done.

  1. React js: gulp serve
  2. React Native: react-native run-android / run-ios

 

Conclusion

This is just the beginning of what this approach can accomplish. In the next article, I will move on to adding logic to this App and add Redux to the mix. This is where the true power of this approach will be seen - we'll create one logic and use it in both Web and the Native App.


Boris Žmaher