首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >返回组件消息5秒,然后重定向

返回组件消息5秒,然后重定向
EN

Stack Overflow用户
提问于 2021-01-20 08:31:35
回答 2查看 388关注 0票数 0

我希望实现这样的情况:如果我使用不正确的查询参数导航到我的URL,就会显示一条消息,然后重定向到另一个页面。

想象一下,当您没有登录时,尝试导航到一个只有登录用户才能看到的页面。我希望它呈现这样的内容:“您需要登录才能看到这个内容”,然后在2-5秒后页面重定向到/login页面。

注意:包含的一些代码只是伪代码。

我知道我可以用简单的三元显示登录页面或重定向。

代码语言:javascript
复制
return hasQueryParams ? <MyLoggedInPage /> : <Redirect to={`/login`} />

然而,我似乎不能让一个setTimeout工作来延迟重定向.

代码语言:javascript
复制
const redirect = () => {
        let redirect = false;

        setTimeout(() => {
            redirect = true;
        }, 5000);

        return redirect
            ? <Redirect to={`/login`} />
            : <h1>Need to be logged in for that</h1>;
    }

return redirect();

为此,我得到了一个错误:Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render. Or maybe you meant to call this function rather than return it

我还试过使用useState

代码语言:javascript
复制
const [redirectNow, setRedirectNow] = useState(false);

useEffect(() => {
    // Some code unrelated to the timeout/redirect
}, []);

const redirect = () => {
        setTimeout(() => {
            setRedirectNow(false);
        }, 5000);

        return redirectNow
            ? <Redirect to={`/login`} />
            : <h1>Need to be logged in for that</h1>;
    }

return redirect();

但这也会产生一个不同的错误:Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: 1. You might have mismatching versions of React and the renderer (such as React DOM) 2. You might be breaking the Rules of Hooks 3

我从进一步阅读中了解到,我们不能从事件处理程序中访问useState内容。

更新

我还应该补充一点,目前我已经将useEffect用于其他事情。

EN

回答 2

Stack Overflow用户

发布于 2021-01-20 08:39:36

interval.

  • Redirect

  • 使用状态来保持是否重定向的跟踪。在更新状态时,

  • 在更新状态后更新状态。

代码语言:javascript
复制
function RedirectComponent() {
  const [shouldRedirect, setShouldRedirect] = React.useState(false);

  React.useEffect(() => {
    const id = setTimeout(() => {
      setShouldRedirect(true);
    }, 5000);

    return () => clearTimeout(id);
  }, []);

  if (shouldRedirect) return <Redirect to="/login" />

  return <h1>This is the message</h1>
}
票数 0
EN

Stack Overflow用户

发布于 2021-01-20 08:43:15

我更喜欢使用custom hook

在这种情况下,useSetTimeout可以工作。

代码语言:javascript
复制
import React, { useState, useEffect, useRef } from 'react';

function useSetTimeout(timeoutCallback, seconds) {
    const timeoutId = useRef();

    useEffect(() => {
        timeoutId.current = setTimeout(timeoutCallback, seconds);
        return () => clearTimeout(timeoutId.current);
    }, [])
}

function YourComponent(){
  const [redirectNow, setRedirectNow] = useState(false);
  useSetTimeout(()=>{
    setRedirectNow(true);
  }, 5000);
  return redirectNow ? <Redirect to={`/login`} /> : <h1>Need to be logged in for that</h1>;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65805734

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档