Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Router redirect IndexRoute to proper path

I'm trying to configure React Router so that when accessing http://url/manage/roomId it goes directly to http://url/manage/roomId/sessions (loading RoomSessions component). These are tabs components' routes and I want to load the first tab's content (which it does) by default with the proper URL (which it does not).

It works fine except for the redirection

<Route
    path="manage/:roomId"
    component={RoomsManagerManageRoom}
    onEnter={requireAuth}
>
    <IndexRoute component={RoomSessions} onEnter={requireAuth} />
    <Route path="sessions" component={RoomSessions} onEnter={requireAuth} />
    <Route path="meetings" component={RoomMeetings} onEnter={requireAuth} />
    <Route path="files" component={RoomFiles} onEnter={requireAuth} />
    <Route path="recordings" component={RoomRecordings} onEnter={requireAuth} />
    <Route path="sections" component={RoomSections} onEnter={requireAuth} />
    <Route path="hosts" component={RoomHosts} onEnter={requireAuth} />
</Route>

What am I missing?

like image 739
Gab Avatar asked Mar 02 '16 22:03

Gab


People also ask

How do I redirect a path in react?

import { Redirect } from "react-router-dom"; The easiest way to use this method is by maintaining a redirect property inside the state of the component. Whenever you want to redirect to another path, you can simply change the state to re-render the component, thus rendering the <Redirect> component.

How do I change the default path in react router?

Use the Navigate element to set a default route with redirect in React Router, e.g. <Route path="/" element={<Navigate to="/dashboard" />} /> . The Navigate element changes the current location when it is rendered.

How do I redirect using useNavigate?

You can use the function returned by the useNavigate hook in two ways. Pass the path you want to navigate as the first argument and an optional object as the second argument. Pass the delta in the history stack to which you want to navigate as the only argument.

What are two ways of handling redirect with react router?

Two ways to handle redirecting on a user event such as create, update and delete with React Router.


1 Answers

Replace the <IndexRoute /> line with

<IndexRedirect to="sessions" />
like image 191
rguerrettaz Avatar answered Oct 17 '22 16:10

rguerrettaz