Ionic 5 React Tutorial: Build a Mobile App with Ionic 5, Axios and React

Ionic 5 React Tutorial: Build a Mobile App with Ionic 5, Axios and React

In this post we will give you information about Ionic 5 React Tutorial: Build a Mobile App with Ionic 5, Axios and React. Hear we will give you detail about Ionic 5 React Tutorial: Build a Mobile App with Ionic 5, Axios and ReactAnd how to use it also give you demo for it if it is necessary.

In this tutorial we’ll be using Ionic 5, Axios and React to build a news application. We’ll be making use of NewsAPI.org for getting news data and we’ll be using the Axios client to send API calls to the News REST API.

Prerequisites

You need to have some prerequsisites before tackling this tutorial:

  • Knowledge of JavaScript,
  • Basic knowledge of React is necessary since we’ll be using React as the frontend library,
  • You need to have Node.js and NPM installed on your development machine. It’s required by the create-react-app tool that will be used to generate a React project and start the local development server.

Installing create-react-app

If you have npm installed on your machine. Open a terminal and run the following command to install create-react-app globally on your system:

$ npm install -g create-react-app

As the time of this writing, create-react-appv2.1.8 will be installed.

Creating a React Project

Next, let’s use create-react-app to generate a React project. Head back to your terminal and run the following command:

$ create-react-app react-ionic-app

Next, navigate to your project’s root folder and start the local development server using the following commands:

$ cd react-ionic-app$ npm start

You server will be running from the http://localhost:3000/ address.

Adding Ionic to our React app

Now we need to install Ionic for React available from the @ionic/react package. We’ll also install the react-router package since Ionic for React depends on it. Open a new terminal, navigate to your React project and run the following commands:

$ npm install @ionic/react react-router react-router-dom --save 

Note: At the time of this writing, @ionic/reactv0.0.5(beta), react-routerv5.0.0 and react-router-domv5.0.0 will be installed.

How to Use Ionic 5 Components in React

Open the src/App.js file and import Ionic 5 styles:

importReact,{Component}from'react';// Import Ionic 5 stylesimport'@ionic/core/css/core.css';import'@ionic/core/css/ionic.bundle.css';import'./App.css';

At this point, you can simply import the desired Ionic 4 component and use it in your application.

Let’s import the following commponents:

import{IonApp,IonContent,IonHeader,IonTitle,IonToolbar,IonCard,IonCardContent,IonCardHeader,IonCardSubtitle,IonCardTitle,IonButton}from'@ionic/react';

Next, change the render() method to create to create an Ionic 5 UI with a card:

classAppextendsComponent{render(){return(<IonApp><IonHeader><IonToolbarcolor="primary"><IonTitle>NewsAppv1.</IonTitle></IonToolbar></IonHeader><IonContent><IonCard><IonCardHeader><IonCardTitle>Newstitle</IonCardTitle><IonCardSubtitle>Newssubtitle</IonCardSubtitle></IonCardHeader><IonCardContent><IonButton>Read</IonButton></IonCardContent></IonCard></IonContent></IonApp>);}}exportdefaultApp;

This is a screenshot of our UI at this point:

Ionic 4 & React UI

Installing Axios in Your React Project

We’ll be using the Axios client to send Ajax requests to the News REST API.

Head back to your terminal and install Axios from npm using the following command:

$ npm install axios --save

As of this writing axiosv0.18.0 will be installed on your project.

Getting News Data

Head over to the news API, register a new account a take note of your API key. Next, open the src/App.js file and add the following variables which hold the API key and URL:

classAppextendsComponent{API_KEY='YOUR_API_KEY';API_URL='https://newsapi.org/v2/top-headlines?sources=techcrunch&apiKey=${this.API_KEY}';

Next, import axios and add the componentDidMount() method where you can send an API call to the News API:

importaxiosfrom'axios';classAppextendsComponent{API_KEY='YOUR_API_KEY';API_URL='https://newsapi.org/v2/top-headlines?sources=techcrunch&apiKey=${this.API_KEY}'state={items:[]}componentDidMount(){axios.get(this.API_URL).then(response=>response.data).then((data)=>{this.setState({items:data.articles})console.log(this.state.items)})}render(){return(<IonApp><IonHeader><IonToolbarcolor="primary"><IonTitle>NewsAppv1.</IonTitle></IonToolbar></IonHeader><IonContent>{this.state.items.map((item)=>(<IonCard><imgsrc={item.urlToImage}/><IonCardHeader><IonCardTitle>{item.title}</IonCardTitle><IonCardSubtitle>{item.author}</IonCardSubtitle></IonCardHeader><IonCardContent><p>{item.content}</p><IonButtonhref={item.url}>Read</IonButton></IonCardContent></IonCard>))}</IonContent></IonApp>);}}exportdefaultApp;

We store the articles in the items state variable after we fetch them with Axios. In the render() method we loop through the items state and display information related to each news article using an Ionic 5 card component.

This is a screenshot of our app at this point:

Ionic 4/React app

Conclusion

In this tutorial we used Ionic 5 with React and Axios to build a simple news application with a mobile UI. At this time, Ionic/React is still at beta, we’ll be adding more tutorials later when it reaches a stable release.


Hope this code and post will helped you for implement Ionic 5 React Tutorial: Build a Mobile App with Ionic 5, Axios and React. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US