1
import React, {Component} from 'react';



import {DateRangePicker, SingleDatePicker, DayPickerRangeController} from 'react-dates';
import 'react-dates/lib/css/_datepicker.css';

class Clients extends Component {
  constructor(props) {
    super(props);
    this.state = {
      focusedInput: '',
      startDate: '',
      endDate: ''
    };
    this.onDatesChange = this.onDatesChange.bind(this)
    this.onFocusChange = this.onFocusChange.bind(this)
  }
  onDatesChange({startDate, endDate}) {

    this.setState({startDate, endDate});
  }

  onFocusChange(focusedInput) {
    this.setState({focusedInput});
  }

  render() {
    const {focusedInput, startDate, endDate} = this.state;

    return (<div className="animated fadeIn">


      <div>
        <DateRangePicker
          onDatesChange={this.onDatesChange} 
          onFocusChange={this.onFocusChange}
          focusedInput={focusedInput}
          startDate={startDate}
           endDate={endDate}/></div>

    </div>);
  }
}

export default Clients;

the errors are : arning: Failed prop type: Invalid input type: startDate of type string supplied to DayPickerRangeController, expected object. in DayPickerRangeController (created by DateRangePicker) in DateRangePicker (created by withStyles(DateRangePicker)) in withStyles(DateRangePicker) (created by Clients) in div (created by Clients) in div (created by Clients) in Clients (created by Route) in Route (created by Full) in Switch (created by Full) in div (created by Container) in Container (created by Full) in main (created by Full) in div (created by Full) in div (created by Full) in Full (created by Route) in Route in Switch in Router (created by HashRouter) in HashRouter

this happens when i click on startdate to pick a date any help ?

5
  • 1
    try to use moment library, passing a new moment object to react-dates , moment(). Then you can asign the start date you need using moment. Commented Mar 1, 2018 at 14:18
  • how should i use react-moments with daterangepicker ? Commented Mar 1, 2018 at 14:21
  • install moment with npm or yarn ( npm i moment --save), import moment on your component, and render react-dates with startDate={moment()}. Commented Mar 1, 2018 at 14:36
  • it is now working thank you Commented Mar 1, 2018 at 15:01
  • Ok I'll put it like an answer, please validate it Commented Mar 1, 2018 at 15:27

1 Answer 1

3

The problem was solved using moment library to set startDate

installing moment:

 npm i moment --save
 yarn add moment

import moment at your component:

import moment from 'moment'

set startDate at react-dates with a moment object

startDate={moment()}
Sign up to request clarification or add additional context in comments.

2 Comments

another question :
I need to have a key the use it when I change the date let's say I have multiple date range pickers how could I find which is what I have changed something like that onDatesChange({startDate, endDate , Key}) { this.setState({startDate, endDate}); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.