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

242
Visualizações
Is using an if statement to redirect user away if page does not exists good practice in React?

Basically, I am trying to route a user based on whether or not the page exists. So I tried to do an if statement to check the prop profileExists. If it is true, it would stay on the same page. Otherwise, it would reroute the user away to another route, which is the /homepage route. I was wondering if this is an ok thing to do in React or not. Please see code below:

renderWithRoot(component) {
  const { classes, profileExists, history } = this.props;

  if (profileExists === false) {
    history.push('/homepage');
  }

  return (
    <div className={classes.root}>
      {component}
    </div>
  );
}
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

It is ok, but not quite in the way you are using it. You should either issue the redirect as an imperative navigation in a useEffect hook

useEffect(() => {
  if (!profileExists) {
    history.replace('/homepage');
  }
}, [profileExists]);

...

return (
  <div className={classes.root}>
    {component}
  </div>
);

or issue the redirect as a declarative navigation via rendering the Redirect component.

if (!profileExists) {
  return <Redirect to='/homepage' />;
}

return (
  <div className={classes.root}>
    {component}
  </div>
);
about 4 years ago · Santiago Gelvez 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