Home > Software engineering >  React Link To button timeout
React Link To button timeout

Time:03-10

I have a simple button that when I click him he redirect me to another page in the website now using react router ( <Link to=''>). now what's I want is when I click the button he wait 3 seconds and then redirect me how can I accomplish this?

Thanks

CodePudding user response:

Sounds like you want to use setTimeout. If you wrap setTimeout around your button's onClick method then you will be able to introduce a delay.

For example, here's a button which when you click it it waits 3 seconds (3000 milliseconds) before opening Stack Overflow in another tab:

<button
  onClick={() => setTimeout(() => window.open("https://stackoverflow.com/"), 3000)}
>
  Click me!
</button>

CodePudding user response:

<button
  onClick={() => setTimeout(() => window.open("url", '_blank'), 3000)}
>
  I will run after 3 seconds
</button>

This will work!

or try with the useNavigate in react-router-dom

  • Related