Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

458
Visualizações
react-router-dom useNavigate() hook, id is not getting set in url path

I am using react-router-dom v6 for routing purpose. What I am trying to do is passing id in navigate url path

Following is my onClick method and button code

  let navigate = useNavigate();
  const routeChange = (id) => {
    let path = `${PATH_DASHBOARD.general.ViewActivity}` + id;
    navigate(path);
  };
---------------------------------------------------------------------------
<Button
                          type="submit"
                          variant="contained"
                          sx={{
                            width: "100%",
                            marginTop: 3,
                          }}
                          onClick={() =>
                            routeChange(item.id)
                          }
                        >
                          <Typography variant="body2" align="center">
                            View
                          </Typography>
                        </Button>

Here is my index.js, I am using useRoutes hook

    {
      path: "other",
      element: (
        <AuthGuard>
          <DashboardLayout />
        </AuthGuard>
      ),
      children: [
        {
          path: `/my-activities`,
          element: <MyActivities />,
        },
        {
          path: `/my-activities/view-activity/:id`,
          element: ({ id }) => <ViewActivity id={id} />,
        },
      ],
    }

But when I try to access url is the browser on button click, I am getting this

http://localhost:3000/other/my-activities/view-activity/:id252515

Anyone knows the solution?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You are only appending an id value to the end of your path string.

Use the generatePath utility to inject the id value into the path: "/my-activities/view-activity/:id" path string.

generatePath

import { generatePath, useNavigate } from 'react-router-dom';

...

const navigate = useNavigate();
const routeChange = (id) => {
  const path = generatePath(PATH_DASHBOARD.general.ViewActivity, { id });
  navigate(path);
};
about 4 years ago · Juan Pablo Isaza Relatório

0

You are just adding id value at the end of your path, you should replace it

 let path = PATH_DASHBOARD.general.ViewActivity.replace(':id',id);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda