Getting Started With Ionic React πŸ˜‡

Introduction

 
In this article, we are going to learn about Ionic React and how to create an application using Ionic React step by step.
 

What is Ionic React?

 
Recently the Ionic company introduced a new open source library called Ionic React that helps to build Android applications as well as iOS, desktop and the web as a Progressive Web App. This library is a web component based library. Version 4.11 is the first official version of Ionic React.
 
In the past, Ionic basically works with Angular technology but now they also supprt React as well. In look and feel, the style of the Ionic React component is especially great on Native apps as well as desktop apps.
 

Ionic React vs React Native

 
Ionic React
  1. Helps to build hybrid mobile applications as well as to build desktop applications.
  2. It allows using web technology like HTML, CSS, and JavaScript.
  3. Performance is much  less as compared to React Native.
  4. Uses Cordova plugins to build an Application.
  5. Developed by Drifty.co.
React Native
  1. Helps to build cross platform mobile apps natively. 
  2. Uses React and JavaScript to build an application but at runtime it renders Native UI element.
  3. Better application performance.
  4. Has its own API to build an Application.
  5. Developed by Facebook.
  6. Instagram, UberEats, Myntra are some popular brands which uses React Native

Getting Started with First Ionic React App

 
Before starting with Ionic React app, make ensure that your system has installed following configuration
Step 1
 
Install Ionic CLI by using below command (Open command prompt and install)
  1. npm i -g ionic  
 
Step 2 - Create a project
 
Create a folder where we are actually placing our project
 
Go to this folder directory
 
Run the below command to create an application
  1. ionic start my-first-app  
 
It's very interesting to see here, CLI is asking for "Select JavaScript framework to use your new app". You have to select React framework to create Ionic React App using keyboard arrow key and then press enter button.
 
 
Once you press the enter button, CLI will ask "which starter template you would like to use in your project." like the below picture.
 
 
You can select any one option from the above. In this case, I am going to create "A blank starter project". So, for that, I have selected first blank template option for the project and press enter button. Now, the CLI will create an application and install all the dependencies (files required for the project).
 
Step 3 - Run Application
 
Once you have completed the above steps, go into the project directory and run the application using the below commands,
  1. cd my-first-app  
  2. ionic serve  
Once you have executed the above commands, first your project gets compiled and then starts a dev server and open your application on the browser like below picture.
 
Step 4
 
Open your project into VS code or your favorite editor and see the project structure like the below picture.
 
 
In this project, you might notice that your file extension is .tsx. it means Ionic React uses typescript to write the code. You can use .js extension file as well.
 
src - This folder contains all the code related to the application. 
pages - this folder is just a container of separate components. e.g.: - about, home, contact component
theme - this folder contains CSS files of project. a variables.css file is there.
Index.tsx - this file is main entry point of the project and render App component of App.tsx file.
App.tsx - in this file, we have imported React from react library and used here. Similarly, other libraries like for Route and Redirect imported “react-router-dom” library. 
 
Step 5 - Test Application
 
Open Home.tsx file and replace the following code into it.
  1. import {  
  2.   IonContent, IonHeader, IonPage, IonTitle, IonToolbar, IonCard,  
  3.   IonCardTitle, IonCardContent, IonCardHeader, IonCardSubtitle, IonGrid, IonRow, IonCol  
  4. } from '@ionic/react';  
  5. import React from 'react';  
  6.   
  7. const Home: React.FC = () => {  
  8.   return (  
  9.     <IonPage>  
  10.       <IonHeader>  
  11.         <IonToolbar color="dark">  
  12.           <IonTitle>C# Corner App Using Ionic React</IonTitle>  
  13.         </IonToolbar>  
  14.       </IonHeader>  
  15.       <IonContent className="ion-padding">  
  16.         <IonGrid>  
  17.           <IonRow>  
  18.             <IonCol size="6">  
  19.               <IonCard color="tertiary">  
  20.                 <IonCardHeader>  
  21.                   <IonCardSubtitle>C# Corner</IonCardSubtitle>  
  22.                   <IonCardTitle>About C# Corner</IonCardTitle>  
  23.                 </IonCardHeader>  
  24.   
  25.                 <IonCardContent>  
  26.                   C# Corner is an online global community of 3 million software developers.  
  27.       </IonCardContent>  
  28.               </IonCard>  
  29.             </IonCol>  
  30.           </IonRow>  
  31.         </IonGrid>  
  32.       </IonContent>  
  33.     </IonPage>  
  34.   );  
  35. };  
  36.   
  37. export default Home;  
Now, run the application by using the below command and check the output for this.
  1. ionic serve  
Output
 

Summary

 
In this article, I discussed what is Ionic React. After that, we checked the difference between Ionic React and React Native. At the end of this article, we saw set code for Ionic React.


Similar Articles