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

171
Visualizações
useParams returns undefined

See edit 1

I am trying to use useParams() hooks but it keeps returning undefined value.

This is the url

http://localhost:3000/cart/?id=61a00439d03ef6261c127997&qty=1&size=36x48

import { useParams } from "react-router-dom";

export default function CartScreen(props) {
  let { qty, id, size } = useParams();
  console.log("slug : " + qty);
  console.log("id : " + id);
  console.log("size : " + size);

  ...
}

enter image description here

Actually I don't really know why it console.logs this many time but anyway, it is undefined and I don't know why.

Edit 1 :

  const router = useRouter();
  const productId = router.query.id;
  const qty = router.query.qty;
  const size = router.query.size;

  console.log("productId : " + productId);
  console.log("qty : " + qty);
  console.log("size : " + size);

returns enter image description here

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

0

Your url isn't in the format the useParams lib needs it. If this doesn't help you, check if you defined the routes correctly.

Have a look at the answer in this post:

Using the useParams hook in react

about 4 years ago · Juan Pablo Isaza Relatório

0

use URLSearchParams checkout this: How to get query string using React?

export default function CartScreen(props) {
   const query = new URLSearchParams(window.location.search);
   const qty = query.get('qty');
   const id = query.get('id');
   const size = query.get('size');
   console.log("slug : " + qty);
   console.log("id : " + id);
   console.log("size : " + size);

...
}

or use hook useQuery() checkout this: https://v5.reactrouter.com/web/example/query-parameters

import { useQuery } from "react-router-dom";

export default function CartScreen(props) {
   const query = useQuery();
   const qty = query.get('qty');
   const id = query.get('id');
   const size = query.get('size');
   console.log("slug : " + qty);
   console.log("id : " + id);
   console.log("size : " + size);

...
}

and try change http://localhost:3000/cart/?id=xxx&qty=xxx&size=xxx to http://localhost:3000/cart?id=xxx&qty=xxx&size=xxx is best practice and more readable.

about 4 years ago · Juan Pablo Isaza Relatório

0

Let me answer with a simple vanilla js method in case you prefer not to use a library.

function urlParamsObj(source) {
    /* function returns an object with url parameters
    URL sample: www.test.com?var1=value1&var2=value2
    USE: console.log(URLparamsObj().var2)  --> output: value2
    You can use it for a url-like string also: urlParamsObj("www.ok.uk?val_a=2&b=3").val_a*/
    var urlStr = source ? source : window.location.search ? window.location.search : ""
        if (urlStr.indexOf("?") > -1) { // if there are params in URL
            var param_array = urlStr.substring(urlStr.indexOf("?") + 1).split('&'),
            theLength = param_array.length,
            params = {},
            i = 0,
            x;
            for (; i < theLength; i++) {
                x = param_array[i].toString().split('=');
                params[x[0]] = x[1];
            }
            return params;
        }
        return {};
}

Usage is just the same: let { qty, id, size } = urlParamsObj();

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