Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

249
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda