Failed to setState({ date }) on onDateChange
See original GitHub issueNeed help, for the following code:
import React from 'react';
import { SingleDatePicker } from 'react-dates';
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
focused: false,
date: null,
otherKey1: '',
otherKey2: 1,
};
this.onChangeDate = this.onChangeDate.bind(this);
this.onChangeDateFocus = this.onChangeDateFocus.bind(this);
}
onChangeDate(date) {
console.log(date);
this.setState({
...this.state,
date,
}, () => console.log(this.state));
}
onChangeDateFocus({ focused }) {
this.setState({
...this.state,
focused,
});
}
render() {
const { date, focused, otherKey1, otherKey2 } = this.state;
return (
<SingleDatePicker
id="my-data-picker"
date={date}
focused={focused}
onDateChange={this.onChangeDate}
onFocusChange={this.onChangeDateFocus}
/>
);
}
}
When click on any date, it will trigger onFocusChange
, but I will get focused=null
.
More importantly, when click on any date, despite it successfully trigger onDateChange
with date=MomentObject{...}
, the this.setState
fails. My this.state.date
is always null
.
Any ideas? Do I need to require('moment')
for this file?
Issue Analytics
- State:
- Created 7 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Failed to setState({ date }) on onDateChange #346 - GitHub
Need help, for the following code: import React from 'react'; import { SingleDatePicker } from 'react-dates'; class MyComponent extends ...
Read more >setState is undefined when onDateChange for DatePicker
If I use it gives setState is undefined.Can anyone help in debugging this error. Thanks in advance.* import React, {useState} from 'react'; ...
Read more >DatePicker - Calcite React
Navigate forward to interact with the calendar and select a date. Press the question mark key to get the keyboard shortcuts for changing...
Read more >react-native-calendar-picker - npm
onDateChange = this.onDateChange.bind(this); } onDateChange(date, type) { if (type === 'END_DATE') { this.setState({ selectedEndDate: date ...
Read more >setState is undefined when onDateChange for DatePicker ...
const [date, setDate] = useState('06 Jul 2020') //usage onDateChange={(newDate) => setDate(newDate)}. Hagai Harari 2928. score:0.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
anyway, it magically healed it self today, strange. closing
@zengqingfeng1994 please file a new issue; this one is 2 years old.