question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Failed to setState({ date }) on onDateChange

See original GitHub issue

Need 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:closed
  • Created 7 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
SiqiTian-mintedcommented, Feb 28, 2017

anyway, it magically healed it self today, strange. closing

0reactions
ljharbcommented, Aug 1, 2019

@zengqingfeng1994 please file a new issue; this one is 2 years old.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found