# Ionic react router trouble

**URL:** <https://forum.ionicframework.com/t/ionic-react-router-trouble/228879>\
**Category:** Ionic React\
**Created:** [November 22, 2022, 7:41pm UTC](https://forum.ionicframework.com/t/ionic-react-router-trouble/228879 "2022-11-22T19:41:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![minotheo](https://avatars.discourse-cdn.com/v4/letter/m/7ab992/32.png) [@minotheo](https://forum.ionicframework.com/u/minotheo)\
**Post date:** [November 22, 2022, 7:41pm UTC](https://forum.ionicframework.com/t/ionic-react-router-trouble/228879/1 "2022-11-22T19:41:57Z")

</div>

Hello, i tried to make router with auth and have a trouble. When i authorize in app and component Router re-renders url stay /login, i tried to redirect from not implemented page to /home, and the problem is when i try to navigate to profile which have params, params when i go to the page profile is null, and i noticed when i delete Redirect component and manually go to the profile page via tab button it work correctly and params is an object with id for example in my case.

```auto
const Router: React.FC = () => {

    const dispatch = useAppDispatch();

    const {isAuth, currentUser} = useAppSelector(state => state.userReducer)

    useEffect(() => {
        if(localStorage.getItem('accessToken'))
            dispatch(userRefresh())
    }, [])

    if(!isAuth)
        return (
            <IonReactRouter>
                <IonRouterOutlet animated={false}>
                    <Route path="/login" component={Login}/>
                    <Route path="/recovery" component={Recovery}/>
                    <Route path="/register" component={Register}/>

                    <Route render={() => <Redirect to="/login"/>}/>
                </IonRouterOutlet>
            </IonReactRouter>
        );

    return (
        <IonReactRouter>
            <IonTabs>
                <IonRouterOutlet animated={false}>
                    <Route path="/home" component={Home}/>
                    <Route path="/music" component={Music}/>
                    <Route path="/messages" component={Messages}/>

                    <Route path="/profile/:id" component={Profile}/>
                    <Route path="/dialog/:id" component={MessageBox}/>

                    <Redirect to={"/home"}/>
                </IonRouterOutlet>

                <IonTabBar slot="bottom" className={"tabs ion-padding-vertical"}>
                    <IonTabButton tab="home" href="/home">
                        <IonIcon icon={homeOutline} />
                    </IonTabButton>

                    <IonTabButton tab="messages" href="/messages">
                        <IonIcon icon={chatbubbleOutline} />
                        <IonBadge color="success">180</IonBadge>
                    </IonTabButton>

                    <IonTabButton tab="music" href="/music">
                        <IonIcon icon={musicalNoteOutline} />
                    </IonTabButton>

                    <IonTabButton tab="profile" href={`/profile/${currentUser!.id}`}>
                        <IonIcon icon={personOutline} />
                    </IonTabButton>
                </IonTabBar>
            </IonTabs>
        </IonReactRouter>
    );
};

```

---

<div class="post-metadata">

**Author:** ![minotheo](https://avatars.discourse-cdn.com/v4/letter/m/7ab992/32.png) [@minotheo](https://forum.ionicframework.com/u/minotheo)\
**Post date:** [November 25, 2022, 7:08pm UTC](https://forum.ionicframework.com/t/ionic-react-router-trouble/228879/2 "2022-11-25T19:08:17Z")

</div>

Problem still exists. UP.

---

<div class="post-metadata">

**Author:** ![ptmkenny](https://avatars.discourse-cdn.com/v4/letter/p/ea666f/32.png) [@ptmkenny](https://forum.ionicframework.com/u/ptmkenny)\
**Post date:** [November 26, 2022, 10:21am UTC](https://forum.ionicframework.com/t/ionic-react-router-trouble/228879/3 "2022-11-26T10:21:19Z")

</div>

I don’t think you can use a `<Redirect>` component when conditionally rendering two different `<IonReactRouter>`. After awaiting the login result, try using `useHistory()` and `history.push()` instead of a `<Redirect>`.

---

<div class="post-metadata">

**Author:** ![ptmkenny](https://avatars.discourse-cdn.com/v4/letter/p/ea666f/32.png) [@ptmkenny](https://forum.ionicframework.com/u/ptmkenny)\
**Post date:** [November 26, 2022, 12:34pm UTC](https://forum.ionicframework.com/t/ionic-react-router-trouble/228879/5 "2022-11-26T12:34:31Z")

</div>

You might be able to use `useHistory()` with `useEffect()` in a custom hook.

Another approach is to refactor your router and use one router, not two, and then redirect the user if a user who has not logged in tries to access an authorized route.

Here’s an example from an app I built:

```auto
                    <Route
                      exact
                      path={routeUserCancel}
                      render={(props) =>
                        handleAuthRoute(userObject, <PageUserCancel />)
                      }
                    />

const handleAuthRoute = (userObject: User, component: JSX.Element) => {
  if (userObject.isLoggedIn) {
    // eslint-disable-next-line react/jsx-no-useless-fragment
    return <>{component}</>;
  }
  return <RedirectToLogin />;
};

```

---

<div class="post-metadata">

**Author:** ![minotheo](https://avatars.discourse-cdn.com/v4/letter/m/7ab992/32.png) [@minotheo](https://forum.ionicframework.com/u/minotheo)\
**Post date:** [November 26, 2022, 2:34pm UTC](https://forum.ionicframework.com/t/ionic-react-router-trouble/228879/6 "2022-11-26T14:34:34Z")

</div>

Oh, this way seems good, thanks, I’ll give it a try.

---

<div class="post-metadata">

**Author:** ![minotheo](https://avatars.discourse-cdn.com/v4/letter/m/7ab992/32.png) [@minotheo](https://forum.ionicframework.com/u/minotheo)\
**Post date:** [November 26, 2022, 3:30pm UTC](https://forum.ionicframework.com/t/ionic-react-router-trouble/228879/7 "2022-11-26T15:30:14Z")

</div>

Thats a lot, i finally fixed that.
