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

312
Visualizações
React Router Dom, redirect with an error message to login page and show the message there

I am using React Router v6 in my application. The user should be redirected to the login page when not logged in and an error message should be shown. So far the redirection part is working well and it takes the user to the login page. I need to show the authentication required message in the login page.

The code is as follows:

const PrivateRoute = ({ Component }) => {
  const auth = false; //your logic

  return auth ? (
    <Component />
  ) : (
    <Navigate
      to="/login"
      replace={true}
      state={{ alert: "Authentication is required" }}
    />
  );
};

The login component is as follows:

function Login(props) {
  debugger
  return <h1>Login page</h1>;
}

How do I access the error message in the login component? Something like this.props.alert ?

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

0

You could access the alert message with the help of useLocation from React Router Dom. Like this:

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

function Login(props) {
  const location = useLocation();
  return <h1>{location.state.alert}</h1>;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

you can access it like:

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

Then:

const { state } = useLocation();
return <div>{state.alert}</div>;
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