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

277
Visualizações
How to redirect route with id to not found if id doesn't exist in react router

I have a route with id patients/:id. I'm typing route in a browser like this patients/10 and it shows data of a patient with id 10. The problem is when I'm typing an id that does not exist in DB it returns blank data. My expectation is, that it returns to page 404 or something like that. Is there a way for me to do that?

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Yes you can achieve this thing by placing a condition in your API call, i.e, if your API throws an error of 404 then you simply redirect to 404-page using this.props.history.push('/404'), this is an example you can use this as a reference.

about 4 years ago · Santiago Trujillo Relatório

0

Yes, this is possible. I'm assuming that your React frontend is querying the server for each patient.

Upon making the request on the page, you could check to see if the response body is empty. If it is empty, you could redirect to a 404. If there is data, you can continue to print data on the page.

const isEmpty = Object.keys(obj).length === 0;
if isEmpty(response) {
    return <Redirect to="/your-404-route" />
}
// continue to print details below

about 4 years ago · Santiago Trujillo Relatório

0

For this first, you check the data that exits in the database or not. If the data is exiting in the DB then it redirects as patients/10 URL otherwise its redirects to /error-page (error page route name).

about 4 years ago · Santiago Trujillo 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