So, I’m trying to build a button which when clicked toggles between the 25 minute timer, and 5 minute break timer. On click the button text changes fine, and if I check React Dev Tools, the correct prop of 1500 seconds/300 seconds is being passed into the Timer component, however the state does not update. When I click the reset button the state is then updated and the correct time value is displayed. Why is the correct props being passed to Timer but the Timer state is not being updated by the following code?
The conditionally rendered component is in the code below stored in the timer variable.
It’s hard to say without seeing the toggleBreak function. Timer’s state has to be changed in order for it to re-render. I’m not familiar with this challenge, but you may need to attach a function to the button that updates Timer’s state. Do you know how to this.setState( () => { return {prop: newProp}; ?
Sorry, I can’t figure it out! I notice it does render correctly with the Reset button, though. You could probably use that.
It looks to me that you are correctly passing the state down through the props, and when the props change, it should re-render (I thought). But I’m rather new to React , so there must be something I’m missing.
If you’re setting props to state you have to listen to props change. In class based components it’s usually done via componentDidUpdate.
Add this to your Timer component: