How do you set a default route in Ionic React with React Router?

← Ionic answers

Inside IonRouterOutlet, add <Redirect exact from="/" to="/home" />. In plain React Router 6, use <Navigate to="/home" replace /> on the index route.

// Ionic React (React Router 5 style API)
import { IonRouterOutlet } from '@ionic/react';
import { IonReactRouter } from '@ionic/react-router';
import { Redirect, Route } from 'react-router-dom';
 
<IonReactRouter>
  <IonRouterOutlet>
    <Route exact path="/home" component={Home} />
    <Route exact path="/settings" component={Settings} />
    <Redirect exact from="/" to="/home" />
  </IonRouterOutlet>
</IonReactRouter>
// Plain React Router 6
import { Navigate, Route, Routes } from 'react-router-dom';
 
<Routes>
  <Route index element={<Navigate to="/home" replace />} />
  <Route path="/home" element={<Home />} />
  <Route path="*" element={<NotFound />} />
</Routes>

Which API you use depends on the router version your Ionic packages are built for, so check @ionic/react-router in your project. Use exact on the redirect so that it matches only the root path.

Related questions

Start debugging smarter today

Try Bugsee for free — no commitments, no hidden fees.

Free sign upLive demoNo credit card required