最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - REACTJS redirecting + 5 second timeout - Stack Overflow

programmeradmin1浏览0评论

Im fairly new to reactjs and can't figure something out. I am trying to build a webpage that redirects after 5 seconds but can't seem to figure out how. If anyone has any tips for me where I can find out how to do this it would be really appreciated.

Thanks :)

Im fairly new to reactjs and can't figure something out. I am trying to build a webpage that redirects after 5 seconds but can't seem to figure out how. If anyone has any tips for me where I can find out how to do this it would be really appreciated.

Thanks :)

Share Improve this question asked Aug 17, 2020 at 8:28 M4R10M4R10 331 silver badge4 bronze badges 2
  • What exactly have you tried? – TOLULOPE ADETULA Commented Aug 17, 2020 at 8:57
  • Well I tried to push history and get it to the correct path but for some reason my puter won't let me use history even though its in the sed folder. I am just exploring new methods of achieving the same thing. – M4R10 Commented Aug 17, 2020 at 9:56
Add a ment  | 

3 Answers 3

Reset to default 3

That's something you can do with plain JavaScript. It would look like this:

setTimeout(function() {
  window.location.replace('some-url');
}, 5000);

Depending on when you want to fire that, you can put it in some ponent's "lifecycle method" or even just simply add it at the beginning of your JS file or in 'DOMContentLoaded' event.

const nav = useNavigate();
useEffect(()=>{ 
    setTimeout(()=> nav('/login'),5000);
  },[])

Not sure of your use case but you can simply do that using setTimeout

https://www.w3schools./jsref/met_win_settimeout.asp

and you want to have this in ponentDidMount().

read more about lifecyles here https://reactjs/docs/state-and-lifecycle.html

发布评论

评论列表(0)

  1. 暂无评论