Skip to main content

React Native First Application Hello World

Let's build our first React Native application on Windows as development operating system and Android as target operating system.

Steps to create React Native application

1. First, you have to start your emulator (Android Emulator) and make it live.

2. Create a directory (ReactNative) in your any drive.

3. Open "Command Prompt" and go to your ReactNative directory.
4. Write a command react-native init FirstApp to initialize your app "FirstApp".

React Native First Application Hello World

5. Go to your directory location "FirstApp" and run the command react-native run-android. It will start Node server and launch your application in virtual device emulator.

React Native First Application Hello World
React Native First Application Hello World

Output:

React Native First Application Hello World

View Code of React Native Application

Open your one of the favorite JavaScript supportable IDE and open App.js file inside your FirstApp application.

React Native First Application Hello World

Code of default React Native first app.

App.js

  1.  import React, {Component} from 'react';  
  2. import {Platform, StyleSheet, Text, View} from 'react-native';  
  3.   
  4. const instructions = Platform.select({  
  5.   ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',  
  6.   android:  
  7.     'Double tap R on your keyboard to reload,\n' +  
  8.     'Shake or press menu button for dev menu',  
  9. });  
  10.   
  11. type Props = {};  
  12. export default class App extends Component<Props> {  
  13.   render() {  
  14.     return (  
  15.       <View style={styles.container}>  
  16.         <Text style={styles.welcome}>Welcome to React Native!</Text>  
  17.         <Text style={styles.instructions}>To get started, edit App.js</Text>  
  18.         <Text style={styles.instructions}>{instructions}</Text>  
  19.       </View>  
  20.     );  
  21.   }  
  22. }  
  23.   
  24. const styles = StyleSheet.create({  
  25.   container: {  
  26.     flex: 1,  
  27.     justifyContent: 'center',  
  28.     alignItems: 'center',  
  29.     backgroundColor: '#F5FCFF',  
  30.   },  
  31.   welcome: {  
  32.     fontSize: 20,  
  33.     textAlign: 'center',  
  34.     margin: 10,  
  35.   },  
  36.   instructions: {  
  37.     textAlign: 'center',  
  38.     color: '#333333',  
  39.     marginBottom: 5,  
  40.   },  
  41. });  

index.js

  1. /** @format */  
  2.   
  3. import {AppRegistry} from 'react-native';  
  4. import App from './App';  
  5. import {name as appName} from './app.json';  
  6.   
  7. AppRegistry.registerComponent(appName, () => App);  

Create a simple React Native "Hello World" application

Create a simple "Hello World" app by editing App.js file of FirstApp.
Save the application and reload by simply pressing twice "R" or Ctrl+M (Reload).

App.js

  1. import React, {Component} from 'react';  
  2. import {Platform, StyleSheet, Text, View} from 'react-native';  
  3.   
  4. type Props = {};  
  5. export default class App extends Component<Props> {  
  6.   render() {  
  7.     return (  
  8.       <View>  
  9.         <Text>Hello World</Text>  
  10.       </View>  
  11.     );  
  12.   }  
  13. }  
Output:

React Native First Application Hello World

Edit simple React Native "Hello World" application

Edit React Native "Hello World" application using StyleSheet.

App.js

  1. import React, {Component} from 'react';  
  2. import {Platform, StyleSheet, Text, View} from 'react-native';  
  3.   
  4. type Props = {};  
  5. export default class App extends Component<Props> {  
  6.   render() {  
  7.     return (  
  8.       <View>  
  9.         <Text style={styles.welcome}>Hello World</Text>  
  10.       </View>  
  11.     );  
  12.   }  
  13. }  
  14. const styles = StyleSheet.create({  
  15.   welcome: {  
  16.     fontSize: 20,  
  17.     textAlign: 'center',  
  18.     margin: 10,  
  19.   }  
  20. });  
Output:

React Native First Application Hello World

React Native Code Explanation

  • import React, {Component} from 'react': imports the library and other components from react module and assign then to variable React.
  • const instruction: sets to display the platform-specific message.
  • export default class App extends Component: defines the classes that extend the React Component. The export default class modifier makes the class "public". This block of code defines the components that represent the user interface.
  • render(): a function that returns a React element.
  • return(): returns the result of layout and UI components.
  • View: a container that supports the layout accessibility controls. It is a fundamental component for building the UI.
  • Text: a React component for displaying text.
  • style: a property used for styling the components using StyleSheet.
  • styles: used to design individual components.
  • {styles.instructions}>{instructions}:
  • const styles = StyleSheet.create({}): The StyleSheet class creates the style object that controls the layout and appearance of components. It is similar to Cascading Style Sheets (CSS) used on the Web.

Comments

Popular Posts

Reloading UITableView while Animating Scroll in iOS 11

Reloading UITableView while Animating Scroll Calling  reloadData  on  UITableView  may not be the most efficient way to update your cells, but sometimes it’s easier to ensure the data you are storing is in sync with what your  UITableView  is showing. In iOS 10  reloadData  could be called at any time and it would not affect the scrolling UI of  UITableView . However, in iOS 11 calling  reloadData  while your  UITableView  is animating scrolling causes the  UITableView  to stop its scroll animation and not complete. We noticed this is only true for scroll animations triggered via one of the  UITableView  methods (such as  scrollToRow(at:at:animated:) ) and not for scroll animations caused by user interaction. This can be an issue when server responses trigger a  reloadData  call since they can happen at any moment, possibly when scroll animation is occurring. Example of s...

What are the Alternatives of device UDID in iOS? - iOS7 / iOS 6 / iOS 5 – Get Device Unique Identifier UDID

Get Device Unique Identifier UDID Following code will help you to get the unique-device-identifier known as UDID. No matter what iOS user is using, you can get the UDID of the current iOS device by following code. - ( NSString *)UDID { NSString *uuidString = nil ; // get os version NSUInteger currentOSVersion = [[[[[UIDevice currentDevice ] systemVersion ] componentsSeparatedByString: @" . " ] objectAtIndex: 0 ] integerValue ]; if (currentOSVersion <= 5 ) { if ([[ NSUserDefaults standardUserDefaults ] valueForKey: @" udid " ]) { uuidString = [[ NSUserDefaults standardDefaults ] valueForKey: @" udid " ]; } else { CFUUIDRef uuidRef = CFUUIDCreate ( kCFAllocatorDefault ); uuidString = ( NSString *) CFBridgingRelease ( CFUUIDCreateString ( NULL ,uuidRef)); CFRelease (uuidRef); [[ NSUserDefaults standardUserDefaults ] setObject: uuidString ForKey: @" udid " ]; [[ NSUserDefaults standardUserDefaults ] synchro...

Xcode & Instruments: Measuring Launch time, CPU Usage, Memory Leaks, Energy Impact and Frame Rate

When you’re developing applications for modern mobile devices, it’s vital that you consider the performance footprint that it has on older devices and in less than ideal network conditions. Fortunately Apple provides several powerful tools that enable Engineers to measure, investigate and understand the different performance characteristics of an application running on an iOS device. Recently I spent some time with these tools working to better understand the performance characteristics of an eCommerce application and finding ways that we can optimise the experience for our users. We realised that applications that are increasingly performance intensive, consume excessive amounts of memory, drain battery life and feel uncomfortably slow are less likely to retain users. With the release of iOS 12.0 it’s easier than ever for users to find applications that are consuming the most of their device’s finite amount of resources. Users can now make informed decisions abou...