有条件的路由对日期

发布于 2025-02-12 04:02:28 字数 113 浏览 1 评论 0原文

我正在ReactJ中构建一个网站,该网站允许用户在工作日填写表格,但在周末重定向到封闭页面。我如何使用React-Router-dom有条件地路由到其他页面?我也希望它不是当地时间,但是根据PST的说法。 谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆で 2025-02-19 04:02:28

在不知道您的确切路由器设置的情况下,我只能猜测,但这里有一些想法:

  1. usenavigate钩子:

      import {usenavigate}从“ react-router-dom”;
    
     const navigate = usenavigate();
    
     //根据语言环境检查当前日期 
     useeffect(()=> {
        //日期检查逻辑
        // if(工作日){导航('//dyday/exter/page'); }
        //如果(周末){导航('/week多/特定/页面'); }
        //其他导航('//Why Why Why Why)
     },[]);
    
     

注意使用> useeffect的使用,您希望在安装决定是否应执行重定向的组件时检查逻辑。

  1. < redirect/>组件:

      import {redirect,route}来自'react-router-dom';
     // const工作日=(日期检查逻辑)
     <路由exkert path =“/”>
       {工作日? <重定向为=“ /weptay” /> :<周末 />}
     </route>
     

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>
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文