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

274
Visualizações
How to use either react router useParams() or window.localStorage()

In my scenario , the component should take a value either from the URL or from localStorage but when I attempt to get value from useParams() it throws this error:

Uncaught ReferenceError: useParams is not defined

My code is:

const { userId } = useParams() || props;
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

I think you didn't import useParams, in which case do so at the top of your file :

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

Then do :

let {userId} = useParmas();
if(!userId) userId = props.userId;

Because useParams return an object, an empty object like so {} if there isn't any parameter, but {} is not false, so by doing so useParams() || props, if there isn't an userId parameter in you your url, you will always get undefined.

about 4 years ago · Juan Pablo Isaza Relatório

0

Make sure that you have imported useParams. If you have imported it already, then try doing this.

let { userId } = useParams();
if(!userId) userId = props.userId;
about 4 years ago · Juan Pablo Isaza Relatório

0

If you are using a class component, you must use withRouter() instead.

import { withRouter } from "react-router";


class Whatever extends Component {
  render() {
    const { userId } = this.props.match.params;
    return null;
  }
}
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