Skip to content

Need help/hints to improve my implementation of react-dates #1785

@maxwaiyaki

Description

@maxwaiyaki

I'm using a functional component with hooks to create a Datepicker component for my react app but I keep getting the below warning from react.

Warning: State updates from the useState() and useReducer() Hooks don't support the second callback argument. To execute a side effect after rendering, declare it in the component body with useEffect().

import React, { useState  } from 'react';
import { DateRangePicker } from 'react-dates';
import 'react-dates/initialize';
import 'react-dates/lib/css/_datepicker.css';

function Datepicker() {
  const [dateRange, setdateRange] = useState({
    startDate: null,
    endDate: null
  });
  const [focus, setFocus] = useState(null);

  const { startDate, endDate } = dateRange;

  const handleOnDateChange = (startDate, endDate) =>
    setdateRange(startDate, endDate);

  return (
      <DateRangePicker
        startDatePlaceholderText="Start"
        startDate={startDate}
        onDatesChange={handleOnDateChange}
        endDatePlaceholderText="End"
        endDate={endDate}
        numberOfMonths={1}
        displayFormat="MMM D"
        showClearDates={true}
        focusedInput={focus}
        onFocusChange={focus => setFocus(focus)}
        startDateId="startDateMookh"
        endDateId="endDateMookh"
        minimumNights={0}
      />
  );
}

export default Datepicker;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions