Please checkout and subscribe to my video content on YouTube. Feel free to leave comments and suggestions for what content you would like to see. YouTube Channel
Ionic React (Beta) Tabs: Step By Step
Working with the new ionic cli generating an app with tabs and eventually a login page
use the ionic cli to build your app, make sure you specify react and we are going to use the tab starter as our baseline and then move some things around to get the desired results.
See blog post for more detailed instructions on getting started Blog Post Here
Enter into console, and when prompted select tabs as the starter template
$ ionic start myApp --type=react
? Starter template: tabs
Housecleaning
So lets clean up some of this and create a more structured starting point.
Create a new file called TabRoot.tsx and copy everything from inside of the IonApp element in App.tsx over to the new component. When you are done, App.tsx should look like this
And TabRoot.tsx should look like this after pasting the code we cut from App.tsx.
Please note I have removed the imports to save space when looking at the code. I also have reduced the number of tabs from three to two, I believe that is sufficcient to make my point.
Please note that the documentation regarding how IonTabs work in React doesnt appear to be correct
Now the application is set up such that the default route is to render the TabRoot component, but we need to tell the component which tab to render and we want it to be Tab1
Having all of the default routing based around tabs at the route level of the application can become problematic as the application gets more complex. As you will see in the later sections when the app has to check for authenticated user and protected routes, this setup will be beneficial
Cleanup Tab1
There is alot of noise in Tab1 so lets make it look like Tab2, copy contents from Tab2 into Tab1
Lets just duplicate the file Tab1.tsx and rename it Tab1Detail.tsx... clean it up so it looks like this when you are done.
// FILE: Tab1Detail.tsximportReactfrom'react';import{IonHeader,IonToolbar,IonTitle,IonContent}from'@ionic/react';constTab1Detail:React.SFC=()=>{return (<><IonHeader><IonToolbar><IonTitle>Tab One Detail</IonTitle></IonToolbar></IonHeader><IonContent/></>);};exportdefaultTab1Detail;
Add button in the IonContent section of Tab1; we will use that button to navigate to the detail page Tab1Detail that we just created.
// FILE: Tab.tsx<IonContent><IonButtonexpand="full"style={{margin:"14"}}onClick={e=>{e.preventDefault();props.history.push("/tab1-detail");}}> NEXT PAGE</IonButton></IonContent>
So a few problems when you make this change in Tab1.tsx, the first one is
Where is the props.history coming from?
We can use react-router withRouter to get the history object passed along as a property to the component since the component was being rendered by the Router. So lets make the following changes to the files.
// FILE: Tab1.tsx// add the import..import{withRouter}from"react-router";
Then add parameter, and for now we will specify the type as any
Now to go back, we need to first add the IonBackButton component to the toolbar on the Tab1Detail page, right above the <IonTitle>.
// FILE: Tab1Detail.tsx<IonButtonsslot="start"><IonBackButtontext=""defaultHref="/"onClick={()=>props.history.replace("/tab1")}goBack={()=>{}}/></IonButtons><IonTitle>Tab One Detail</IonTitle>
Known Issue: As of now, the defaultHref is not working so I had to respond to the onCLick event to get this to work.
As you can see we are using the history propery again to go back to the previous component so we need to add the withRouter and properly specify the parameters for the component.
// FILE: Tab1Detail.tsximport{withRouter}from"react-router";// <== NEWconstTab1Detail:React.SFC<any>=(props)=>{// <== NEWreturn (<><IonHeader><IonToolbar><IonButtonsslot="start"><IonBackButtontext=""defaultHref="/tab1"onClick={()=>props.history.replace("/tab1")}// <== NEWgoBack={()=>{}}/></IonButtons><IonTitle>Tab One Detail</IonTitle></IonToolbar></IonHeader><IonContent/></>);};exportdefaultwithRouter(Tab1Detail);// <== NEW