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
3 Answers
Reset to default 3That'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