Acer79
Acer79

Reputation: 213

React passing props to components

I am trying to pass in props to a component which works while using componentWillReceiveProps, but once the counter is done it calls clearInterval(this.intervalId);However once I change the input again the counter does not get initiated again. How can i pass the updated props back to the component?

Component code;

class Stopwatch extends Component {
    constructor(props) {
       super(props);
        this.state = {
            currentCount: this.props.counter,
            hours: 0,
            minutes: 0,
            seconds: 0
        }
}

componentWillMount() {
  this.timer(this.props.counter);
}

timer() {
  this.setState({
    currentCount: this.state.currentCount - 1
})

  const seconds = Math.floor(this.state.currentCount % 60);
  const minutes = Math.floor((this.state.currentCount/60) % 60);
  const hours = Math.floor((this.state.currentCount/3600) % 3600);

  this.setState({hours, minutes, seconds});

  if (this.state.currentCount < 1) {
    clearInterval(this.intervalId);
  }
}

componentDidMount() {
    this.intervalId = setInterval(this.timer.bind(this), 1000);
} 

leading0(num) {
    return num < 10 ? '0' + num : num;
}

componentWillReceiveProps(nextProps){
  if(nextProps.counter !== this.props.counter){
    this.setState ({currentCount: nextProps.counter})
  }
}

render() {
    return (
        <div>
            <div>Hours {this.leading0(this.state.hours)}</div>
            <div>Minutes {this.leading0(this.state.minutes)}</div>
            <div>Seconds {this.leading0(this.state.seconds)}</div>
        </div>
     )

Main Code;

class App extends Component {
constructor(props) {
    super(props);
    this.state = {
        deadline: 'December 25, 2018',
        newDeadline: '',
        counter: 75,
        newCounter: ''
    };
}

changeDeadline() {
    this.setState({deadline: this.state.newDeadline});
}

changeNumber(e) {
    this.setState({counter: this.state.newCounter});
}

render() {
    return (
        <div className='App'>
            <div className='App-title'>Countdown to {this.state.deadline}</div>
            <Clock 
                deadline={this.state.deadline}
            />
            <Form inline>
                <FormControl
                    className="Deadline-input"
                    placeholder='New Date'
                    onChange={event => this.setState({newDeadline: event.target.value})}
                />
                <Button onClick={() => this.changeDeadline()}>Submit</Button>
            </Form>

            <div>Stopwatch From { this.state.counter } Seconds</div>
            <Stopwatch 
                counter={this.state.counter}
            />
            <Form inline>
                <FormControl
                    className="Deadline-input"
                    placeholder='New Number'
                    onChange={event => this.setState({newCounter: event.target.value})}
                />
                <Button onClick={() => this.changeNumber()}>Submit</Button>
            </Form>
        </div>
    )

}

Thanks in Advance

Upvotes: 1

Views: 66

Answers (1)

Farnabaz
Farnabaz

Reputation: 4066

componentDidMount function calls once, if you want to reset counter on props change, you should do it in componentWillReceiveProps function

class Stopwatch extends Component {
    // ...
    resetInterval() {
        if (this.intervalId) {
            clearInterval(this.intervalId);
            this.intervalId = null;
        }
        this.intervalId = setInterval(this.timer.bind(this), 1000);
    }
    componentWillReceiveProps(nextProps){
        if(nextProps.counter !== this.props.counter){
            this.setState ({currentCount: nextProps.counter})
            // reset interval
            this.resetInterval()            
        }
    }
    //...
}

Upvotes: 1

Related Questions