I am encountering an issue with the DayPicker component when trying to manage external state in a custom component. Specifically, the Toggle component behaves inconsistently when included as part of a custom Caption in DayPicker. The state changes are not handled correctly, and the onCheckedChange event is called multiple times incorrectly.
Steps to Reproduce
Set up a DayPicker component with a custom Caption that includes a Toggle component.
Manage the toggle state outside of the DayPicker component.
Expected Behavior
The Toggle component should change state correctly and call the onCheckedChange handler only once per toggle action.
Actual Behavior
The Toggle component calls the onCheckedChange handler multiple times per action, leading to inconsistent state changes.
When using the Toggle component standalone outside the DayPicker, it works as expected. However, integrating it within the DayPicker causes unexpected behavior. I suspect this might be due to how DayPicker manages its state and re-renders custom components.
Using a React.Context fixes the issue, but I find it really ugly:
Description
I am encountering an issue with the
DayPicker
component when trying to manage external state in a custom component. Specifically, theToggle
component behaves inconsistently when included as part of a customCaption
inDayPicker
. The state changes are not handled correctly, and theonCheckedChange
event is called multiple times incorrectly.Steps to Reproduce
DayPicker
component with a customCaption
that includes aToggle
component.DayPicker
component.Expected Behavior
The
Toggle
component should change state correctly and call theonCheckedChange
handler only once per toggle action.Actual Behavior
The
Toggle
component calls theonCheckedChange
handler multiple times per action, leading to inconsistent state changes.Minimal Reproducible Example
Additional Context
When using the
Toggle
component standalone outside theDayPicker
, it works as expected. However, integrating it within theDayPicker
causes unexpected behavior. I suspect this might be due to howDayPicker
manages its state and re-renders custom components.Using a React.Context fixes the issue, but I find it really ugly:
Environment
react-day-picker
version: ^8.10.1Screenshots / Recordings
https://github.com/gpbl/react-day-picker/assets/37446572/1c090305-75e6-412a-ab52-dd3fa9e3a01f
Links