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!
Without knowing your exact router setup, I can only guess, but here are some ideas:
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.
<Redirect /> component:
import { Redirect, Route } from 'react-router-dom';
// const weekday = (date checking logic)
<Route exact path="/">
{weekday ? <Redirect to="/weekday" /> : <Weekend />}
</Route>