Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

250
Views
How to properly manage routes with react-router-dom?

I've had a problem with redirecting to page via react routing. I want all my private paths to start with /client/:id and then add another part to this path when I'clicking onto dashboard menu. Finall path should be something like this: /client/:id/home.

So, i start to coding and change my object handling all paths like this:

export const routes = {
    home: "/client/:id/home"
}

and handling componenet object like this:

export const routing = [
    { path: routes.home, component: Home }
]

all of this i use in my App.tsx like below:

{routing.map(({ path, component }, i) => (
   <Route key={i} path={path} component={component} />
))}

And right now I've got a problem. After redirecting to home with using react-router-dom history like below:

const history = useHistory();
history.push(`/client/${id}/home`)

router redirect me onto 404 page :(

Can someone tell me what i'm doing wrong?

Thanks for any help!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Thank you for the question. For routing the basic principle is same in case of React, Angular, Vue. Using typescript or JavaScript. When navigating we do not need to useHistory and push history in it. You can use nested routing here and achieve the same. export const routes = { home: "/client/:id/home" } Please find attached example.

[https://codepen.io/shwetas7/pen/rNGRNdW][1]
about 4 years ago · Juan Pablo Isaza Report

0

This is an example with the functionality that you need. You have to use history.go(); after history.push to reload the page.

/*
Question: How to properly manage routes with react-router-dom?
Url: https://stackoverflow.com/questions/70730898/how-to-properly-manage-routes-with-react-router-dom/70731195#70731195
 */

import { Redirect, Route, Router, Switch, useParams } from 'react-router-dom';
import { createBrowserHistory } from 'history';

const history = createBrowserHistory();

function Route2({ history, match }) {
  // id = 1
  // const { id } = match.params;
  const { id } = useParams();
  return (
    <>
      <div>Route 2</div>
      <button onClick={() => {
        history.push("/");
        history.go();
      }}> navigate to Route 1</button>
    </>
  );
}

function Question4() {
  return (
    <>
      <Router history={history}>
        <Switch>
          <Route
            path="/client/:id/home"
            exact
            render={(props) => <Route2 {...props} />}
          />
          <Route
            path="/"
            render={({ history }) => {
              return (
                <>
                  <div>Route 1</div>
                  <button onClick={() => {
                    // history.push("/client/1/home");
                    // history.go();
                    window.location.href = '/client/1/home'
                  }}> navigate to Home</button>
                </>
              )
            }}
          />
          <Redirect path="/" />
        </Switch>
      </Router>
    </>
  );
}

export default Question4;

I hope I've helped you

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!