2
constructor(){
    super()

    this.state = {
        countDown: 6
    }}
componentDidMount(){

    this.myInterval = setInterval(() =>{
        browserhistory.push("/time-up")componentDidUpdate(){
    if(this.state.countDown === 0){
      browserHistory.push("/time-up");
}
}

//I want to redirect the user to a page called "time up" when the countdown gets to zero in the componentDidUpdate lifecycle

ravibagul91
  • 20,072
  • 5
  • 36
  • 59
  • 3
    Possible duplicate of [How do I redirect to another webpage?](https://stackoverflow.com/questions/503093/how-do-i-redirect-to-another-webpage) – VLAZ Jun 13 '19 at 05:40
  • Off topic: Shouldn't you be using `setTimeout` instead? Interval is for repeating events. If you insist, for some reason, on using interval, it should at least be cleared before you redirect the user, with `clearInterval(this.myInterval)` – Adam Gerthel Jun 13 '19 at 06:00

4 Answers4

0

Instead of browserHistory.push("/time-up");, add window.location = "/time-up"

Daniel Doblado
  • 2,481
  • 1
  • 16
  • 24
0

You're trying to redirect to /time-up so I guess you're trying to do a internal navigation. You can do it by using the history prop coming from react-router

Instead of browserHistory.push("/time-up"); you can write this.props.history.push("/time-up")

If you don't have the history prop, you can inject by augmenting your component with the withRouter HOC

Milenko
  • 529
  • 3
  • 5
0

To redirect from componentDidUpdate() use this,

componentDidUpdate(){
    if(this.state.countDown === 0){
       window.location.href = "/time-up"
    }
}

Another way is to use Redirect from react-router-dom package,

import { Redirect } from 'react-router-dom';

componentDidUpdate(){
    if(this.state.countDown === 0){
       <Redirect to={'/time-up'} />
    }
}

Note: Your code snippet is messy, you are using componentDidUpdate inside componentDidMount? If yes, then you should not do that. Both are different methods and cannot be called inside another, place both differently.

ravibagul91
  • 20,072
  • 5
  • 36
  • 59
-1

you need to decrement the time or it will sit at 6