I have a react app where in the constructor I initialize my displayDate to an empty string. When the app loads, the displayDate value gets set based on the props.
constructor(props){
this.calendar = null;
this.state{
displayedDate: ""
};
}
componentDidMount(){
this.calendar = new Calendar({
format: "MM/DD/YYYY",
value: this.state.displayedDate
});
}
componentWillMount(){
if(this.props.defaultDate){
this.setState({
displayedDate: this.convertDateToString(this.props.defaultDate)
});
}
}
// many many more lines of code that follow this
render(){
return(
<input type="text"></input>
<i class="fa fa-calendar" id="calendar"></i>
);
}
In testing the app, I set a breakpoint on this.setState
in componentWillMount
and it does indeed enter the if
block, so the app is receiving the date value from props. But setState doesn't seem to be setting the displayedDate
to state. And my convertDateToString
function returns a value, so its not an issue of displayedDate
getting a void value. In componentDidMount
I set a breakpoint and dumped the state value and displayedDate
is still an empty string.
I've seen people experience similar issues like this before, where setState doesn't work as expected. What am I doing wrong? Thanks