0

I have a page which uses React Router V5. There is a general section and there is a section specifically for user profiles. As I have a different page structure there, I used nested routes for the account section. The SideBar is what the name implies and contains buttons which can be used to navigate the account pages /account/profile, /account/settings, as well as navigate to pages outside the nested switch - namely /help.

The App used to be structured roughly like this:

// in index.js
const appDiv = document.getElementById("app")
render(<App />, appDiv)

// in App.js
const App = () => {
    return (
        <Router>
        <div className={styles.pageContainer}>
            <Header />
            <Switch>
                <Route exact path='/' component={() => <HomePage link='about' />} />
                <Route path='/about' component={AboutPage} />
                <Route path='/help' component={HelpPage} />
                <Route path='/log-in' component={LoginPage} />
                <Route path='/sign-up/:signupType' component={SignUpPage} />
                <Route path='/account' component={AccountRouter} />
            </Switch>
            <Footer />
        </div>
    </Router>

}

// in AccountRouter.js
const AccountRouter = () => {
    return (
        <div className={styles.container}>
            <SideBar />
            <Switch>
                <Route path='/account/settings' component={AccountSettingsPage} />
                <Route path='/account/profile' component={ProfileSettingsPage} />
                <Route exact path='/account' component={ProfileSettingsPage} />
            </Switch>
        </div>
    );
};

// in SideBar.js
const SideBar = () => {
    const history = useHistory();

    return (
        <div>
            <button onClick={() => history.push('/account/profile')}>Go to Account Profile</button>
            <button onClick={() => history.push('/account/settings')}>Go to Account Settings</button>
            <button onClick={() => history.push('/help')}>Go to Help Page</button>
        </div>
    )
}

Now it is structured like this:

// in index.js
const appDiv = document.getElementById("app")
render(
    <Provider store={store}>
        <App />
    </Provider>
, appDiv)

// in App.js
// This looks the same

// in AccountRouter.js
// This now has Route protection
const AccountRouter = () => {
    const accountData = useSelector((state) => state.account);

    return (
        <div className={styles.container}>
            {!accountData.isLoggedIn ? <Redirect to='/log-in' /> : null}
            <SideBar />
            <Switch>
                <Route path='/account/settings' component={AccountSettingsPage} />
                <Route path='/account/profile' component={ProfileSettingsPage} />
                <Route exact path='/account' component={ProfileSettingsPage} />
            </Switch>
        </div>
    );
};

// in SideBar.js
// This looks the same.

Before I added Redux, the Sidebar was properly redirecting. After Redux, I have the following behaviour: When the SideBar is outside of the Switch, you can properly navigate to the Help page, but the components don't render, when I try to navigate to the pages inside the AccountRouter Switch. When I move the SideBar into the Switch, the links to the pages inside this switch start working again, but the /help link stops working.

Is there a way of having links to both inside and outside of this Switch in the same SideBar? How could Redux have affected the Router?

Alex V.
  • 180
  • 2
  • 12
  • The `Switch` should only wrap `Route` components, the `Sidebar` should be moved back outside the `Switch` component. I highly doubt Redux has anything at all to do with your routing/navigation issue. Can you edit your post to include all relevant code in a [mcve] so we can see what links you are referring to and what navigation actions you are trying to effect? Are you *really* just trying to implement [route protection](/a/66289280/8690857)? – Drew Reese Oct 10 '22 at 16:14
  • I modified my answer. It is still not a minimal reproducible example, but it should contain all the elements to make it clear what the difference in code is between my commits and how the behaviour differs. I have tried removing the Header and all pages to see if anything there interferes, but there was no change in behaviour. – Alex V. Oct 11 '22 at 10:57
  • Not an answer to my original question, but I upgraded to react router v6 and after applying the neccessary changes, the problem has dissapeared. As I had similar problems with react router v5 before (that time caused by nesting Routers, instead of having only a single one), without any error messages, v6 seems to me at first glance to be more stable and easier to use. – Alex V. Oct 12 '22 at 11:56

0 Answers0