-
-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Example of how to make a controlled component? #4
Comments
Hi, Arjan. The first workaround is correct, you can wrap change of
About
|
Right, I would image that
This works fine for setting the initial state, but if you update the value of const Example = () => {
const [myDates, setMyDates] = useState([new Date(), addDays(new Date(), 3)]);
const dp = useDatePicker({
dates: {
mode: "range",
selectedDates: myDates, // Does not update when `myDates` changes
},
});
return (
<>
<DatePickerUI data={dp.data} propGetters={dp.propGetters} />
<button onClick={() => setMyDates([])}>Clear dates</button>
</>
);
}; |
If you need this feature urgently you can use action |
No worries, thanks for getting back to me. Not needed urgently but it would be nice to have, maybe I'll submit a PR if I get some extra time. |
Hey @ArjanJ. I've been thinking about date-pickers and after some research, I realized that a controlled date-picker is a common pattern. So, I went ahead and made a new release, version 3.0.0, which is fully controlled and available now on our GitHub repository. |
Thank you for this update, it works really well! |
Not an issue, but some guidance would be appreciated on how to set this up so that it works like a controlled component, where the "value" (selected dates) is passed from a parent component or state, and is updated through an onChange handler of sorts. An example of how to use this with
react-hook-form
would be amazing.The only thing I've found so far to react to changes to
selectedDates
is by setting up auseEffect
like this, which honestly doesn't work very well:What would be cool is something like this, where the
onChange
callback is called internally, andselectedDates
is the state value from the parent component that determines what days are selected. (Making this flow of data one way/controlled):Maybe there is a better way to do this, idk, any tips would be appreciated.
The text was updated successfully, but these errors were encountered: