I have a time picker that I want to set the value to this.state.start
. However the value of this.state.start
could be equal to this.props.normal
or this.props.spec
depending on whether the user has set special hours, if they have not then it falls back to using normal hours.
I'm running into an issue trying to do if-else statements to update the state of this.state.start
. While it should update the value correctly (if-else statements should be correct), react doesn't allow you to update the state in the render function like I've written. How can I set this.state.start
conditionally? Code below:
class NormalHours extends React.Component {
constructor(props) {
super(props)
this.state = {
start: null,
}
}
render() {
//Browser is very angry at this part
if(this.props.specStart == this.props.normStart || this.props.specStart == null)
{
//If special hours are null or equal to normal start use normal hours
this.setState({
start: this.props.normStart;
});
}
else
{
//Else the hours are different so use special hours
this.setState({
start: this.props.specStart;
});
}
return(
<div>
//Using Material UI; this is rendered as a textbox
<TimePicker
name="StartTime"
onChange={(e, date) => {
this.props.onSetStartTime(Utils.convertDateToTimeString(date))
}}
value={this.state.start}
/>