Ionic React Documentation
Ionic React Documentation
Along with the UI components, Ionic also provides a command line tool for creating new apps, as
In this guide, we'll go over the basics of both React and Ionic, including any Ionic specific features. If
you're familiar with React, enjoy the guide and learn something new about Ionic. If you're not familiar
with either, no worries! This guide will cover the basics and provide enough information to get an
From here, the global command ionic will allow for the creation of a React project with Ionic and
any other dependencies. To create a new project, run the following command:
From here, we run ionic serve and have our project running in the browser.
we open our project in a code editor and open src/index.tsx , we should see the following:
TSX
So what's going on here? Well, the first three lines are pulling in some dependencies. The first being
React itself. This allows us to write components in an HTML-like syntax called JSX. We'll talk about
The second import is for ReactDOM. The ReactDOM.render method is the browser/DOM specific way
TYPESCRIPT
At first glance, it may look like a lot is going on, so let's break it down, starting with the first group of
imports.
TYPESCRIPT
The next import is from react-router-dom . We're importing Route, which is how we’ll match the
app’s URL with the components we want to render
Following ReactRouter, we next have our first imports for Ionic. To use a component in React, you
must first import it. So for Ionic, this means anytime we want to use a Button or a Card, it must be
added to our imports. In the case of our App component, we're only using IonApp ,
Docs
IonRouterOutlet , and IonReactRouter .
behaves the same as BrowserRouter with a few differences. We have a deeper guide that goes over
The last important import is the Home component import. This is a component that we will be able
to navigate to in our app. We'll look at the navigation part a bit later.
The CSS import is pulling in the utility styles from Ionic for things like padding, typography, etc.
After reviewing all of the imports, we now get to our first look at a React Component:
TYPESCRIPT
This React component sets up the initial routing for our app, as well as include some core Ionic
components for animations and layout (IonRouterOutlet and IonApp). One thing that stands out is
that in React, to do data-binding, the value is passed in curly braces ( {} ). So in the Route
component, we can set the value of component to the Home component from earlier. This is how
React will know that that value is not a string, but a reference to a component.
What's important to note here is that these are all standard React DOM libraries, meaning there's
no custom integration layer or transpilation step.
TYPESCRIPT
Much like the App component we started with, we have some imports for specific Ionic
components, an import for React, and then our React component itself.
IonPage is the base component for all pages (a component with a route/URL), and includes some
common building blocks of a full-screen component, like header, title, and content components.
When creating your own pages, don't forget to have IonPage be the root component for them.
Having IonPage be the root component is important because it helps ensure transitions work
properly as well as provides the base CSS the Ionic components rely on.
IonHeader is a bit self explanatory. It's a component that is meant to exist at the top of the page.
IonHeader itself doesn't do much by itself, aside from handling some flexbox-based layout. It's
IonContent is, as its name suggests, the main content area for our page. It's responsible for
providing the scrollable content that users will interact with, plus any scroll events that could be used
in an app.
Our current content is relatively simple but does not contain anything that could be used in a real
Note: For brevity, we're excluding repeating part of our component, like the function declaration
or import statements for other components.
TYPESCRIPT
Docs
<IonPage>
...
<IonContent>
<IonList>
<IonItem>
<IonCheckbox slot="start" />
<IonLabel>
<h1>Create Idea</h1>
<IonNote>Run Idea by Brandy</IonNote>
</IonLabel>
<IonBadge color="success" slot="end">
5 Days
</IonBadge>
</IonItem>
</IonList>
</IonContent>
</IonPage>
Here in our IonContent , we're adding an IonList and a much more involved IonItem
TYPESCRIPT
<IonItem>
<IonCheckbox slot="start" />
<IonLabel>
<h1>Create Idea</h1>
<IonNote>Run Idea by Brandy</IonNote>
</IonLabel>
<IonBadge color="success" slot="end">
5 Days
</IonBadge>
</IonItem>
Item is important as it clearly shows the mix of React concepts and Web Component concepts. The
first clear example of a React concept is self-closing tags for React Components in IonCheckbox . This
is just a simpler way of writing components that do not contain any child content.
From the Web Components side, we have a special attribute called slot . This is key for letting the
IonItem know where to place the IonCheckbox when it renders. This is not a React API, but a web
standards API.
Let's look at another component from Ionic, FAB. Floating Action Buttons are a nice way to provide a
main action that is elevated from the rest of an app. For this FAB, we'll need three components: a
FAB, a FAB Button, and an Icon.
Docs
TYPESCRIPT
<IonContent>
<IonList>
...
</IonList>
</IonContent>
On our main IonFab , we're setting its positioning with the vertical and horizontal attributes. We're
also setting the render location to "fixed" with the slot attribute. This will tell IonFab to render
Now let's wire up a click handler to this. What we want to do is when we click the button, we'll
navigate to a new page (which we'll create in a moment). To do this, we'll need to get access to React
Router's navigation API. Thankfully since this is rendered in a Router/Route context, we have access
to React Routers APIs via Props passed to our Home component.
TYPESCRIPT
Router, allowing us to push a new route onto the navigation stack. On our IonFabButton , we can
add a click handler, and just call props.history.push and pass in the new route. In this case, we'll
navigate to new .
TYPESCRIPT
and add the new route to our router declaration. Let's open our App.tsx file and add the new route.
TYPESCRIPT
...
import Home from './pages/Home';
With our router now having an entry for the route /new , we'll create the component needed,
TYPESCRIPT
import {
IonBackButton,
IonButtons,
IonContent,
IonHeader,
IonPage,
IonTitle,
IonToolbar
} from '@ionic/react';
import React from 'react';
The content here is pretty straight forward and should look similar to the Home component. What is
new is the IonBackButton component. This is used to navigate back to the previous route. Pretty
Well, in this case, the in-memory history is lost, so the back button disappears. To address this, we
can set the defaultHref attribute value to the URL we want to navigate to if there is no history.
TYPESCRIPT
return (
<IonPage>
<IonHeader>
<IonToolbar>
<IonButtons slot="start">
<IonBackButton defaultHref="/home" />
</IonButtons>
<IonTitle>New Item</IonTitle>
</IonToolbar>
</IonHeader>
Docs
<IonContent />
</IonPage>
);
Here, when we reload, if there is no app history present, we'll be able to navigate back to our home
route.
more detailed look at Ionic’s components, check out the component API pages. For more details on
Previous Next
Overview Lifecycle