Tutorial

Ionic 4.1 and React: Navigation

Published on March 28, 2019
    Ionic 4.1 and React: Navigation

    While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

    Navigating between pages is a core feature of any mobile application. Let’s look at how we can achieve this with React, React Router and Ionic 4.

    For this article, I’m going to assume that you have a React and Ionic 4 application already up and running. If you haven’t done this yet - visit my article on Alligator.io that covers this already.

    The core dependencies we’ll need for this project are the following:

    $ npm i @ionic/core @ionic/react react-router react-router-dom
    

    Creating Our Pages

    For routing to work, we’ll need some pages to route between. Let’s create two new files at src/pages/HomePage.js and src/pages/BlogPage.js

    This allows us to define a HomePage:

    import React from 'react';
    import { IonToolbar, IonTitle, IonContent, IonCard, IonHeader, IonCardHeader, IonCardTitle, IonCardSubtitle, IonCardContent, IonButton } from '@ionic/react'
    
    const HomePage = () => (
      <>
        <IonHeader>
          <IonToolbar color="primary">
            <IonTitle>Home Page</IonTitle>
          </IonToolbar>
        </IonHeader>
        <IonContent>
          <IonCard>
            <IonCardHeader>
              <IonCardSubtitle>Home Page</IonCardSubtitle>
              <IonCardTitle>Let's take a look at the blog</IonCardTitle>
            </IonCardHeader>
            <IonCardContent>
              <p>Sounds like a great idea. Click the button below!</p>
    
              <IonButton>Blog</IonButton>
            </IonCardContent>
          </IonCard>
        </IonContent>
      </>
    )
    export default HomePage
    

    And a BlogPage:

    import React from 'react';
    import { IonToolbar, IonTitle, IonContent, IonCard, IonHeader, IonCardHeader, IonCardTitle, IonCardSubtitle, } from '@ionic/react'
    
    const BlogPage = () => (
      <>
        <IonHeader>
          <IonToolbar color="primary">
            <IonTitle>Blog Page</IonTitle>
          </IonToolbar>
        </IonHeader>
        <IonContent>
          <IonCard>
            <IonCardHeader>
              <IonCardSubtitle>Vue.js</IonCardSubtitle>
              <IonCardTitle>Ionic 4 and Vue.js</IonCardTitle>
            </IonCardHeader>
          </IonCard>
          <IonCard>
            <IonCardHeader>
              <IonCardSubtitle>REACT</IonCardSubtitle>
              <IonCardTitle>Ionic 4 and React</IonCardTitle>
            </IonCardHeader>
          </IonCard>
          <IonCard>
            <IonCardHeader>
              <IonCardSubtitle>ANGULAR</IonCardSubtitle>
              <IonCardTitle>Ionic 4 and Angular</IonCardTitle>
            </IonCardHeader>
          </IonCard>
        </IonContent>
      </>
    )
    export default BlogPage
    

    Defining Routes

    We can then define the routes for our application inside of App.js:

    import React, { Component } from 'react';
    import { BrowserRouter as Router, Route } from 'react-router-dom';
    
    import { IonApp, IonPage, IonRouterOutlet } from '@ionic/react';
    
    import HomePage from './pages/HomePage';
    import BlogPage from './pages/BlogPage';
    
    import './App.css';
    
    class App extends Component {
      render() {
        return (
          <Router>
            <div className="App">
              <IonApp>
                <IonPage id="main">
                  <IonRouterOutlet>
                    <Route exact path="/" component={HomePage} />
                    <Route path="/blog" component={BlogPage} />
                  </IonRouterOutlet>
                </IonPage>
              </IonApp>
            </div>
          </Router>
        );
      }
    }
    
    export default App;
    

    While we could just define our Route with react-router-dom only, placing the Route inside of an IonRouterOutlet will enable animations within route changes.

    Navigating between two pages is therefore as easy as taking history from props and pushing a new Page onto the stack.

    Let’s update the HomePage to accommodate this:

    const HomePage = ({history}) => (
      <>
        <IonHeader>
          <IonToolbar color="primary">
            <IonTitle>Home Page</IonTitle>
          </IonToolbar>
        </IonHeader>
        <IonContent>
          <IonCard>
            <IonCardHeader>
              <IonCardSubtitle>Home Page</IonCardSubtitle>
              <IonCardTitle>Let's take a look at the blog</IonCardTitle>
            </IonCardHeader>
            <IonCardContent>
              <p>Sounds like a great idea. Click the button below!</p>
    
              <IonButton onclick={(e) => {
                e.preventDefault();
                history.push('/blog')}}>Blog</IonButton>
            </IonCardContent>
          </IonCard>
        </IonContent>
      </>
    )
    

    Pretty simple to achieve as you can see! Here’s the results of our work:

    Animated screenshot of our app

    Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

    Learn more about us


    About the authors

    Still looking for an answer?

    Ask a questionSearch for more help

    Was this helpful?
     
    1 Comments
    

    This textbox defaults to using Markdown to format your answer.

    You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

    How to pass value from homepage to blogpage

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Get our biweekly newsletter

    Sign up for Infrastructure as a Newsletter.

    Hollie's Hub for Good

    Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

    Become a contributor

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    Welcome to the developer cloud

    DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

    Learn more
    DigitalOcean Cloud Control Panel