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

101
Visualizações
Conditional routing ReactJS according to date

I'm building a website in ReactJS that allows users to fill out a form during the weekday but redirects to a closed page on the weekend. How would I conditionally route to a different page using react-router-dom? I'd also like it to not be local time, but according to, say, PST. Thanks!

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

0

Without knowing your exact router setup, I can only guess, but here are some ideas:

  1. The useNavigate hook:

     import { useNavigate } from "react-router-dom";
    
     const navigate = useNavigate();
    
     // check the current date, based on locale 
     useEffect(() => {
        // date-checking logic
        // if(weekday) { navigate('/weekday/specific/page'); }
        // if(weekend) { navigate('/weekend/specific/page'); }
        // else navigate('/wherever')
     }, []);
    
    

Note the use of useEffect, you want the logic to be checked upon mounting the component that decides whether the redirect should be performed or not.

  1. <Redirect /> component:

     import { Redirect, Route } from 'react-router-dom';
     // const weekday = (date checking logic)
     <Route exact path="/">
       {weekday ? <Redirect to="/weekday" /> : <Weekend />}
     </Route>
    
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