0

I have a path="/:user" but it also renders in other static pages like /register and /login because react-router thinks it is also a "username". How can I fix this without changing the pathnames of any of my routers? ie making a white list if that's possible?

Wanted behavior:

/1 -> renders 'hi user 1' page

/login -> renders login page

/register -> renders register page

Happening behavior:

/1 -> renders 'hi user 1' page

/login -> renders login and 'hi user login' page

/register -> renders register and 'hi user register' page

My code

ReactDOM.render(
  <AuthProvider>
    <ThemeProvider theme={theme}>
      <Router history={history}>
        <App />
        <Route exact path="/register" component={Register} />
        <Route exact path="/login" component={Login} />
        <Route exact path="/admin" component={Admin} />
        <Route exact path="/:user" component={User} />
      </Router>
    </ThemeProvider>
  </AuthProvider>,
  document.getElementById('root')
);
5
  • It's mostly a case of missing exact on your route paths, please add your router logic in the description Commented Jul 30, 2020 at 7:34
  • Hey thanks for your reply, but I tried playing around with the exact keyword already. Doesn't fix it :| Commented Jul 30, 2020 at 7:37
  • Then please wrap Switch like <Switch> <Router></Router> </Switch> Commented Jul 30, 2020 at 7:47
  • It gives me this error Error: Invariant failed: You should not use <Switch> outside a <Router> Commented Jul 30, 2020 at 8:02
  • 1
    sry, i meant to say wrap the Route with Switch like this <Switch> <Route></Switch>. You could create a codesandbox to better help you Commented Jul 30, 2020 at 8:20

1 Answer 1

1

try using switch. It will stop looking for matches and render the first matched Route. Here's a link to the documentation: https://reactrouter.com/web/api/Switch

import { Route, Switch } from "react-router";

ReactDOM.render(
  <AuthProvider>
    <ThemeProvider theme={theme}>
      <Router history={history}>
        <App />
        <Switch>
          <Route exact path="/register" component={Register} />
          <Route exact path="/login" component={Login} />
          <Route exact path="/admin" component={Admin} />
          <Route exact path="/:user" component={User} />
        </Switch>
      </Router>
    </ThemeProvider>
  </AuthProvider>,
  document.getElementById('root')
);

Sign up to request clarification or add additional context in comments.

3 Comments

Hi thank you for your reply, but when I try this my pages just go blank and renders nothing.
@Jpark9061 could you share error which you see in browser console?
oh wait haha nevermind it worked, I accidentally put the switch key before <App /> which was why it wasn't working. I copy pasted your code and it works! Thank you so much :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.