Set previously saved date to SingleDatePicker on page load.
See original GitHub issueI 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 typestring
supplied toSingleDatePicker
, expectedobject
and a typeerrorTypeError: 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:
- Created 7 years ago
- Comments:5 (1 by maintainers)
Top 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 >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
@ljharb is right, you need to pass a moment JS object rather than a string
date={moment(dateString)}
@rajkapoordev are you passing a string instead of a moment object?