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.

Set previously saved date to SingleDatePicker on page load.

See original GitHub issue

I have saved a date from the SingleDatePicker in db. NO I want to retrieve this date from db and set in the SingleDatePicker. Is that possible. When I try to set the state of date by retrieved date value. A warning shows that

Invalid input type: date of type string supplied to SingleDatePicker, expected object and a typeerror

TypeError: date.format is not a function

value from the date picker I get is 2016-10-07T00:00:00+06:00

my datepicker component code is

import React, { Component } from 'react';
import { SingleDatePicker } from 'react-dates';
import 'react-dates/lib/css/_datepicker.css';
export default class DatePickerBox extends Component {
  constructor(props) {
    super(props);
    this.state = {
      focused: false,
      date: null,
    };
    this.handleOnChange = this.handleOnChange.bind(this);
    this.onFocusChange = this.onFocusChange.bind(this);
  }
  componentWillMount() {
    this.populateValueFromProps(this.props);
  }
  populateValueFromProps(props) {
    const {item} = props;
    const date = item.value;
   this.state = {
      date: date,
    };
    this.props.updateData({
      ...item,
      value: date,
    });
  }
  onFocusChange(data) {
    this.setState({ focused: data.focused });
  }
  handleOnChange = (date) => {
    console.log(date);
    const { item } = this.props;
    this.setState({
      date,
    });
    this.props.updateData({
      ...item,
      value: date.format(item.format)
    });
  }
  enablePrevious() {
    return false;
  }
  render() {
    const { item } = this.props;
    const { focused, date } = this.state;
    return (<div className="rq-rub-element-block">
      <h3 className="rq-rub-shortcode-title">{item.label}</h3>
      <p className="rq-rub-shortcode-subtitle">{item.subtitle}</p>
      <SingleDatePicker
        id={item.id}
        date={date}
        focused={focused}
        onDateChange={this.handleOnChange}
        onFocusChange={this.onFocusChange}
        isOutsideRange={this.enablePrevious}
      />
    </div>);
  }
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
calebkeenecommented, Jun 22, 2018

@ljharb is right, you need to pass a moment JS object rather than a string

date={moment(dateString)}

0reactions
ljharbcommented, Sep 27, 2017

@rajkapoordev are you passing a string instead of a moment object?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set previously saved date to SingleDatePicker on page load.
I have saved a date from the SingleDatePicker in db. NO I want to retrieve this date from db and set in the...
Read more >
Today date or already selected date on previous input not ...
No errors boss.. only thing logic is missing. Cant able to reselect 'already selected dates' or 'today date' in next input field. –...
Read more >
react-dates - npm
The SingleDatePicker is a fully controlled component that allows users to select a single date. You can control the selected date using the...
Read more >
Date Range Picker — JavaScript Date & Time Picker Library
A JavaScript component for choosing date ranges, dates and times. ... setStartDate(Date or string) : Sets the date range picker's currently selected start ......
Read more >
VF Page: Best way to avoid datepicker focus on load?
I have two date fields on my Visualforce page and on occasions the datepicker/calendar pops up upon page load. I understand one way...
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