0

I've implemented react-datepicker in one of my projects. I'm filtering out my data based on the selected month from the react-datepicker.

I want to call a function when I select a date that I am able to successfully use in the onChange? method. The same way I want to call a function when I clear the date using the cross icon in the isClearable method on the date picker component. Is there any way I can call a function on isClearable?

Thanks :)

I am attaching the react-datepicker code I'm using in my code and also a function that I want to call when I deselect the date in the datepicker component.

const testFun = () => {
    console.log("Function calling works on isClearable"); 
    anotherFunCall();
}
<DatePicker
    selected={monthYear}
    dateFormat="yyyy/MM"
    // minDate={}
    maxDate={new Date()}
    onChange={(date) => {
        setMonthYear(date);
        setHide1(false);
    }}
    placeholderText="YYYY-MM"
    showMonthYearPicker
    dropdownMode="select"
    isClearable={testFun}
/>
Luís Mestre
  • 1,851
  • 1
  • 11
  • 29
  • I'm sorry but `isClearable` is not a function prop, it's a boolean as specified [here](https://github.com/Hacker0x01/react-datepicker/blob/master/docs/datepicker.md) in the documentation of the component – Luís Mestre Nov 24 '22 at 08:58
  • What can i do then do have the same functionality? – jeetu gupta Nov 24 '22 at 09:16
  • I think if you put `isClearable={true}` on the component, when you click to clear the date, it should call `onChange` with `null` – Luís Mestre Nov 24 '22 at 09:27
  • I have resolved my issue by using calling the function in an if statement with the state as the condition on which i was storing my date. So whenever the data in state clears the function gets called. Thanks for your valuable reply. :) – jeetu gupta Nov 24 '22 at 10:04

1 Answers1

-1

I have resolved my issue by using calling the function in an if statement with the state as the condition on which i was storing my date. So whenever the data in state clears the function gets called. :)

  • Could you provide a code example of the solution for future reference – Luís Mestre Nov 24 '22 at 11:28
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 27 '22 at 15:10