Sunday, May 14, 2017

Dynamically Creating Your UI using React Native

When building mobile applications, you often need to dynamically create your UI. For example, you are creating a generic conference app and you need to load the tab bar items based on the number of days a conference has. In this case, you need to create the UI at runtime, rather than design time. So how do you do it in React Native? This post shows how it can be done.

Here I have an array simulating a list of sessions for each day:


var sessions = ['iOS', 'Android', 'Windows Phone', 'Tizen']

In the real world you probably will load this from a configuration file or from the Web.

Next, we will add tab bar items to our UI based on the sessions array:

  render() {
    return (
      
          
         {sessions.map((session, i) =>
           
             style={[styles.tabBarButton, this.getStyle({i}) ]} >
             {session}
           
         )}          
        
      
    );
  }

You can use the map() function to iterate through each item in the sessions array, and then add each tab bar buttons to the UI. The getStyle() function allows you to apply unique style for each tab bar item and is defined as follows:

  getStyle(index) { 
     //---index is a dictionary, e.g { i : 1 }         
     switch (index['i']) {

       //---return predefined styles
       case 0: return styles.button1;    
       case 1: return styles.button2;      
       case 2: return styles.button3;      
       
       //---return dynamic style
       case 3: return {backgroundColor: '#FCBF2E'};  
       case 4: return {backgroundColor: '#FC9626'};              
       default: return {backgroundColor: '#FCBF2E'};              
     }
  }

Here, I am demonstrating that the function can return either a predefined style, or you can dynamically create the style if you so desire. 

The styles used by my example are as follows:

     tabBar: {
    flexDirection:'row',   
    height: 50
  },
  tabBarButton: {
    flex: 1,   
    alignItems:'center',
    justifyContent:'center'
  },
  tabBarButtonText:{   
    fontWeight:'bold'   
  },
  button1: { backgroundColor: '#8BC051' },
  button2: { backgroundColor: '#CCD948' },

  button3: { backgroundColor: '#FDE84D' },

The figure below shows the UI with 4 tab bar items and 3 tab bar items (just remove the last item in the sessions variable).

Using react-native-maps for Android - Bug fix for map not showing

If you are using React Native to build your iOS and Android apps, you may need to display a map in your application. For those of you building cross-platform apps, check out the react-native-maps component created by Airbnb - https://github.com/airbnb/react-native-maps. The react-native-maps component is cross-platform and when run on iOS it uses Apple's Maps and Google Maps when run on Android.


There is however one point you need to take note. Due to a bug in the react-native-maps package, you need to perform the following step in order for your android project to link correctly with the react-native-maps package

In the project folder, locate the /node_modules/react-native-maps/ folder and add the following statement in bold to the package.json file:

...
  "rnpm": {
    "android": {
      "sourceDir": "./lib/android"
    }
  },

...

Once this is done, you will be able to link your project correctly:


$ react-native link react-native-maps

One more thing, be sure to apply for your Maps API key from Google and add it into your AndroidManifest.xml file.

Tuesday, May 02, 2017

MOB105 - Cross Platform Mobile Apps Development using React Native

React Native is an open source library from Facebook and Instagram that allows you to write your mobile apps using JavaScript. A React Native app consists of both JavaScript and a native code (either Java for Android, or Objective-C for iOS). The JavaScript code runs in a virtual machine on your mobile device and communicates with the native code through a JSON message passing interface.

With React Native, you code the UI of your mobile app using a set of components in JavaScript. These components are then rendered natively on the respective mobile platform. What's more, React Native supports live updates, allowing your app to be dynamically updated without republishing to the AppStore or Google Play.

In this 2-day course, you will learn how to use React Native to build cross-platform apps for iOS and Android devices.