Skip to content

Using state variable for startMonth doesn't update the day picker #2337

@the-marolie

Description

@the-marolie

If we use a state variable for startMonth, the day picker doesn't update the initial month

Code

import { DayPicker } from 'react-day-picker';
import {useState, useEffect} from  'react';
import 'react-day-picker/dist/style.css';
import "./styles.css";

export default function App() {
  const [startMonth, setStartMonth] = useState('');

  useEffect(() => {
    setStartMonth(new Date(2024,2));
  }, [])

  return (
    <div className="App">
      <DayPicker
          mode="single"
          className="day-picker"
          startMonth={startMonth}
          defaultMonth={startMonth}
        />
    </div>
  );
}

sandbox link - https://codesandbox.io/p/sandbox/strange-benz-vsh96j

Expected Behavior

The initial month in calendar updates if the state variable is updated

Actual Behavior

The initial month displayed s the value set during state variable

Screenshots

image
image

Metadata

Metadata

Assignees

Labels

bugBug or Bug fixes

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions