I just wanted to know why setInterval function is not decrementing the time.... isnt setInterval is suppose to run a function again and again ? I was hoping that If I put in 100 and press submit, it will go down 1 by 1.
class Clock extends Component{
constructor(props){
super(props);
this.state = {
seconds: 60,
}
console.log('this.props',this.props);
console.log('seconds', this.state.seconds);
}
componentWillMount(){
this.timer(this.props.time);
}
componentDidMount(){
setInterval(()=> this.timer(this.props.time),1000);
}
timer(time){
console.log('new time',time )
this.setState({seconds:time-1});
}
render(){
return (
<div>
<div className="Clock-seconds">{this.state.seconds} seconds</div>
</div>
)}}
This is the app.jsx file- user inputs the number in seconds here and it goes to clock as props. I have time in state.
import React, {Component} from 'react';
import Clock from './Clock';
import './App.css';
import {Form, FormControl, Button} from 'react-bootstrap';
class App extends Component{
constructor(props){
super(props);
this.state ={
time: '60',
newTime: ''
}
}
changeTime(){
this.setState({time: this.state.newTime});
}
render(){
return (
<div className="App">
<div className = "App-title">
Countdown to {this.state.time}
</div>
<Clock
time={this.state.time}
/>
<Form inline >
<FormControl
className="Deadline-input"
placeholder='new date'
onChange={event => this.setState({newTime: event.target.value})}
/>
<Button onClick={() => this.changeTime()}>
Submit
</Button>
</Form>
</div>
)
}
}
export default App;