Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Developing a Real World App in Ionic React

Ionic recently added support for the React and Vue frameworks. Traditionally, Ionic used Angular and Cordova frameworks to create their apps. With Ionic 4, we can develop framework-agnostic apps. In this tutorial, we will create a basic Ionic app in the React Framework.

RealWorld and Conduit

The folks at Thinkster are behind a project known as RealWorld project. Conduit is a Medium clone which can be built using multiple stacks. We can mix and match different types of backends and frontends. Every backend and frontend follow the same API spec.

Unlike simple CRUD ToDo Apps, Conduit is a full-fledged CMS. Implementing the project requires some effort.

Getting Started

For this tutorial, we will be covering the following points.

  • Home Page (With and Without Auth)
    • List of Articles
    • Favorite an Article (Auth Required)
    • Navigate to Article Page
    • Navigate to Author Profile Page
    • Display your Feed (Auth Required)
    • Display Popular Tags.
    • Filter Articles by Tags.
  • Article Page (With and Without Auth)
    • Read Article
    • List Comments
    • Add Comment (Auth Required)
    • Navigate to Author Page
  • Settings Page (Auth required)
    • Modify user Settings
  • Profile Page (With and Without Auth)
    • Display User details
    • Display Posts by user
    • Display Posts Favorited by user
    • Follow User (Auth Required)
  • Create Article (Auth Required)
    • Create Article
  • Login / Signup Page
    • Signup User
    • Login User

Understanding the Conduit API

Conduit has a production API endpoint here. We will use that endpoint for this tutorial. Alternatively, you can set up your own backends using a variety of languages.

We will be using:

  • /articles: For fetching articles, comments
  • /users: For creating and Authenticating users
  • /profiles: For following profiles
  • /tags: For fetching tags

Users are authenticated via a JWT token in headers.

Setting up the Project

The Ionic CLI currently doesn't support React templates. We will be using create-react-app to set up our project. Use:

npm i -g create-react-app  
create-react-app conduit --typescript  

Alternatively, we can use npx:

npx create-react-app conduit --typescript  

Navigate to the folder and install Ionic and React router:

cd conduit  
npm install @ionic/react react-router react-router-dom @types/react-router @types/react-router-dom  

Let's initialize Ionic in this project.

ionic init "Conduit" --type=custom  

We need to enable Capacitor in our project

ionic integrations enable capacitor  

A file capacitor.config.json is generated in our project directory. In the file, we need to change the build directory from "webDir": "www" to "webDir": "build".

Create a build folder OR generate a build using:

npm run build  

Now, we can add a platform to our project.

npx capacitor add android

Additionally, we will be using this folder structure.

  • src
    • components/
    • pages/
    • constants.ts

Let's set up our constants file.

export interface AppConfig {  
    API_ENDPOINT : string

  }  

export const CONFIG: AppConfig = {  
    API_ENDPOINT : "https://conduit.productionready.io/api/"
  };

In the App.tsx file, we need to import the Ionic CSS files.

import '@ionic/core/css/core.css';  
import '@ionic/core/css/ionic.bundle.css';  

Developing the App

An offline user has access to the Home, Article, Profile and Login pages. An online user has access to the Home, Article, Profile, Settings and Login Pages.

There are 70+ Ionic components we can use to speed up our development. First, let's set up routing.

Routing

We are using React Router to manage Routing. We will be using IonRouterOutlet to manage routes. The key difference with the website is that users can't navigate with urls. We are also creating a custom component SideMenu for side menu navigation. IonMenuToggle is used to toggle the menu in components.

A loggedIn key and an Event Listener are used to manage user state. The token obtained from authentication will be saved in localstorage.

class App extends Component {  
  constructor(props: any){
    super(props);
  }
  render() {
    return (
      
); } } export default App;

Side menu Component:

class SideMenu extends React.Component {  
  constructor(props: any){
    super(props);
        this.state = {      
      isLoggedIn: localStorage.getItem("isLogin") ? localStorage.getItem("isLogin") :"false",   
      routes:  {
        appPages: [
          { title: 'Home', path: '/', icon: 'home' },         
        ],        
        loggedOutPages: [
          { title: 'Login', path: '/login', icon: 'log-in' },
        ]  }
    }
    window.addEventListener('loggedIn', (e: any) => {            
      this.setState({
        isLoggedIn : e['detail'].toString(),
        routes : {
          appPages: [
            { title: 'Home', path: '/', icon: 'home' },         
          ],
          loggedInPages: [
            { title: 'My Profile', path: '/profile/'+localStorage.getItem("username"), icon: 'person'},        
            { title: 'New Article', path: '/newarticle', icon: 'create' },
            { title: 'Settings', path: '/settings', icon: 'settings' },
            { title: 'Logout', path: '/login', icon: 'log-out' }
          ],
          loggedOutPages: [
            { title: 'Login', path: '/login', icon: 'log-in' },
          ]  }        
      })      

    });  
  }    

   renderMenuItem(menu: any) {
    return (
         {menu.title} 
                   
      )
  }

  render() {
      return (  
        Start Menu
              {this.state.routes.appPages.map((art: any) => this.renderMenuItem(art))}
              {this.state.isLoggedIn === "true" ?  {this.state.routes.loggedInPages.map((art: any) =>
                this.renderMenuItem(art))} > : {this.state.routes.loggedOutPages.map((art: any) =>
                this.renderMenuItem(art))} > }
            
      )
  }
}
export default SideMenu

Home Page

As mentioned above, the Home Page has a list of articles. The /articles endpoint provides us an array of 20 articles.

A normal user can:

  • View a list of articles
  • Navigate to a full article
  • Navigate to author profile
  • Filter articles by tags

An authenticated user can additionally:

  • View his/her article feed
  • Favorite an article

We will create the ArticleCard and TagsCloud components to make our tasks simpler.

Additionally, Header is a wrapper around Ionic Header with title as props.

   {this.props.title}

TagsCloud is a component to retrieve popular tags. We receive an array of tags at the endpoint /tags. We will be passing a function onTagClick in props. So, we will be able to filter the articles using the clicked tag. We are using IonChip to render the tag chips.

handleClick(tag: any) {  
    console.log(tag);
    this.props.onTagClick(tag);
  }

  componentDidMount() {    
    fetch(CONFIG.API_ENDPOINT+"tags")
      .then(res => res.json())
      .then(
        (res) => {
          this.setState({          
            tags: res.tags
          });
        },      
        (err) => {
          console.log(err)
        }
      )
  }

  render() {
    return (      
      
Popular Tags
{this.state.tags.map((tag: any, index: number) => this.handleClick(tag)}>{tag} )}
); } }

The ArticleCard component returns an IonItem and IonItemSliding for logged in users. A user can swipe and favorite an article if he/she chooses to.

Let's create a function loggedOutCard to return an IonItem.

loggedOutCard() {  
  return (                 
           

{this.props.title}

{this.props.author} Read More
) }

We create a function loggedInCard to return an IonSlidingItem.

loggedInCard(){  
  return (        
           
          {this.loggedOutCard()}
        {this.state.favoritesCount}           
  )
}

Authenticated users can favorite an article. We will create a function favoriteArticle to achieve this.

favoriteArticle = (params: any) => {  
  console.log(params);
  let url = CONFIG.API_ENDPOINT+"articles/" + this.props.slug + '/favorite';
  let method;
  if (!this.state.favorited) {
    method = 'POST'
  } else {
    method = "DELETE"
  }
  fetch(url, {
      method: method,
      headers: {
        "Content-Type": "application/json",
        "Authorization": "Token " + localStorage.getItem("token"),
      }
    })
    .then(res => res.json())
    .then(
      (res) => {       
        this.setState({
          favorited: res.article.favorited,
          favoritesCount: res.article.favoritesCount,
        })
      },
      (err) => {
        console.error(err);
      }
    )
}

Finally, let’s return the component based on user state.

  render() {   
      return (
        
        {localStorage.getItem("isLogin") === "true" ? this.loggedInCard() : this.loggedOutCard()} 
        >               
      );    
  }
}

After fetching the articles from the /articles endpoint, we will iterate the ArticleCard component in our Home Page.

render() {  
      return (
           
        Global Feed
              {localStorage.getItem("isLogin") === "true" ? Your Feed : '' }
          
        {this.state.articles.map((article: any) => 
        
        )}      
         this.handleTagClick(e)} >>
      );
    }

The toggle and handleTagClick functions are in the source code.

Article Page

We are fetching an individual article by slug in the article page. The body of the article is in Markdown format. To display the article, we will be using showdown to convert it. Additionally, comments for the article are fetched from the /articles/slug/comments endpoint and logged in users can post a comment. We will be creating a separate Comment component to iterate over the list. To post comments, logged in users can use IonTextArea.

npm i showdown @types/showdown  

We will initialize showdown using:

  this.converter = new Showdown.Converter({
          tables: true,
          simplifiedAutoLink: true,
          strikethrough: true,
          tasklists: true,
          requireSpaceBeforeHeadingText: true
        });  

And we will display markdown display using:

   

The rest of the source can be viewed here.

Login Page

Login Page is a basic form Page. We will be accepting email and password. The /users/login endpoint accepts the POST request and returns a JWT token. We will save the JWT token in localstorage to manage our auth. In order to toggle the side menu, we will be using standard JS events. Also, we will be signing up users using the /users endpoint. Additionally, we are using the IonToast component to display error messages. The code used for the Login Page is below. IonInput uses CustomEvent and we can use the onIonChange event to bind the input to the state.

updateUserName = (event: any) => {  
    this.setState({ username: event.detail.value });  
  };
login= () => {  
    let url , credentials;     
    if(this.state.action  == 'Login'){
      url = CONFIG.API_ENDPOINT + '/users/login';
      credentials = {
        "user": {
          "email": this.state.email,
          "password": this.state.password
      }
      }

    } else {
      url = CONFIG.API_ENDPOINT + '/users';
      credentials = {
        "user": {
          "email": this.state.email,
          "password": this.state.password,
          "username": this.state.username
      }
      }
    }
    fetch(url, {
            method: 'POST',
            headers: {
                "Content-Type": "application/json",              
            },
            body: JSON.stringify(credentials)

        })
        .then((res) => {
          console.log(res);
          if(res.status == 200){
            return res.json();
          } else {  
            if(this.state.action == 'SignUp') {
              throw new Error("Error creating user");
            } else {
              throw new Error("Error Logging in")  
            }                
          }

        } )
        .then(
          (result) => {
              console.log(result);    
              localStorage.setItem("token",result.user.token);       
              localStorage.setItem("username", result.user.username);
              localStorage.setItem("isLogin", "true");
              localStorage.setItem("email", result.user.email);

              this.event = new CustomEvent('loggedIn', {
                detail: true
              });
              window.dispatchEvent(this.event);
              this.props.history.push('/blog');
          },

          (error) => {
           console.error(error);           
           this.setState({toastMessage: error.toString(), toastState: true});
          }
        )
  }

  render(){
    return(
      
    

conduit

this.setState(() => ({ toastState: false }))} message= {this.state.toastMessage} duration={400} >
{this.state.action === 'SignUp' ? : > }
{this.state.action}
Click here to {this.state.action === 'Login'? 'SignUp' : 'Login'} > ) }

New Article Page

This page is a form page like the login page. However, for the article body, we will be using a Markdown editor instead of a text area. For this tutorial, we are going to use react-mde.

npm install --save react-mde  

The form code is as below:

Promise.resolve(this.converter.makeHtml(markdown)) } /> Submit Article

Creating a Build Using Capacitor

We will use run build to build our React project:

npm run build  

We will then use Capacitor to copy the files:

npx capacitor copy  

Now, we will need to compile the project natively:

npx capacitor open android|ios  

Final Thoughts

My mileage while developing Conduit varied a lot. Conduit doesn't have a spec for mobile, so it took a while to figure out the best workflow.

We can improve the app by using middleware like Redux, using skeletons loaders at places, integrating infinite scroll and handling errors better. To complete the app experience, we will have to modify the backend so it can handle push notifications. A notification system makes sense if you receive a new follower or if a person you are following publishes a new post.

As far as Ionic React is concerned, it felt like I was developing a React web application using a set of predefined components. Components like IonInfiniteScroll are still WIP and we can hope to get them soon. Personally, I missed the tight integration of the framework and the side menu template. We can expect full Ionic CLI and Cordova support in the future.

Android users can check the app here.

Lastly, if you're building an application with sensitive logic, be sure to protect it against code theft and reverse-engineering by following our guides for React and for Ionic.



This post first appeared on Jscrambler, please read the originial post: here

Share the post

Developing a Real World App in Ionic React

×

Subscribe to Jscrambler

Get updates delivered right to your inbox!

Thank you for your subscription

×