Reputation: 643
I am having below error in my react component
TypeError: Cannot read property 'startDate' of undefined
I know this is because I didn't bind my function. It works when I have hour
in my localStorage
. But now when I have custom
in my localStorage
. And also it works in componentWillMount either it has hour
or custom
. So here is my code
class Home extends Component {
constructor (props) {
super(props)
this.state = {
filterType: this.getFilterType(localStorage.getItem('daysFilter') ? localStorage.getItem('daysFilter') : 'today'),
}
}
getFilterType (type) {
let filterType = 'hour'
if (type === 'today') {
filterType = 'hour'
}
if (type === "custom" ){
const startDate = this.state.startDate
const endDate = this.state.endDate
}
return filterType
}
}
But I need to know how can I bind my function inside constructor?
Upvotes: 0
Views: 1496
Reputation: 849
When you're instantiating this.state
you are calling getFilterType(type)
which uses this.state
which is not yet initialized.
Upvotes: 1