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

200
Visualizações
How to efficiently get & set url query parameters in React?

Unable to efficiently get & set url query params in React. The app is using react router v4 and hooks such as useHistory, useLocation are not available. I cannot update the react router version since the app is a large production app.

On every feature of the app there are various filters like startDate, endDate and so on. I need a way to:

  1. Check whether any query params are being passed on the initial mount => can be done in useEffect
  2. on changing state of any filter such as startDate or endDate, the url param should change as well.

Currently what I've tried is:

// Init
React.useEffect(() => {
    const params = new URLSearchParams(location.search);
    const startDateParam = params.get('startDate');
    const endDataParam = params.get('endDate');
    const isDog = params.get('isDog'); //example

    if (startDateParam) setStartDate(startDateParam);
    else {
      params.set('startDate', startDate || '');
    } 

    if (endDateParam) setEndDate(endDateParam);
    else {
      params.set('endDate', endDate || '');
    }
},[])

React.useEffect(() => {
   const params = new URLSearchParams(location.search);
   const startDateParam = params.set('startDate', startDate);
   const endDataParam = params.set('endDate', endDate);
},[startDate, endDate]);

const [startDate, setStartDate] = useState(
    moment()
      .subtract(30, 'days')
      .format('YYYY-MM-DD')
  );
const [endDate, setEndDate] = useState(moment().format('YYYY-MM-DD'));

But this is not changing the url the I expect it to.

I need help with changing the url in my current implementation or a completely new (efficient) implementation

about 4 years ago · Juan Pablo Isaza
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