stephy / CalendarPicker

CalendarPicker Component for React Native
803 stars 373 forks source link

onChangeDate not working in hooks #229

Closed shahanshah87 closed 4 years ago

shahanshah87 commented 4 years ago
                  `<CalendarPicker
                    startFromMonday={true}

                    allowRangeSelection={true}

                    minDate={minDate}

                    maxDate={maxDate}

                    todayBackgroundColor={constants.Colors.color_161758}

                    selectedDayColor={constants.Colors.color_ff8148}

                    selectedDayTextColor="#FFFFFF"

                    onDateChange={() => onDateChange()}

                />`

function onDateChange:- const onDateChange = (date, type) => { console.log(date, type); if (type === 'END_DATE') { setData({ ...data, booking_to:date, }) } else { setData({ ...data, booking_from:date, }) } }

I am unable to understand what should I pass as arguement in onDateChange={() => onDateChange(WHAT ARGUEMENT SHOULD BE PASSED HERE?)}

shahanshah87 commented 4 years ago

@stephy @peacechen @kesha-antonov @superandrew213

Roka20012 commented 4 years ago

Hey @Shahanshah87 👋

Could please tell me what exactly doesn't work?

Have you got any errors?

It works well for me

Screenshot 2020-08-18 at 15 30 54

Screenshot 2020-08-18 at 15 31 00
peacechen commented 4 years ago

@Shahanshah87 Refer to the example app: https://github.com/stephy/CalendarPicker/blob/master/example/App.js#L129

shahanshah87 commented 4 years ago

@Roka20012 if you use this in class component, it will work fine but when you use this in functional component. and call the function onDateChange={() => onDateChange()} and const onDateChange = (date, type) => { console.log(date, type); if (type === 'END_DATE') { setData({ ...data, booking_to:date, }) } else { setData({ ...data, booking_from:date, }) } } then in log date and type comes undefined.

peacechen commented 4 years ago

@Shahanshah87 You're not passing any parameters to your onDateChange callback. It's unnecessary to create an anonymous function:

onDateChange={() => onDateChange()}  //  <-- incorrect

Refer to the example app that I linked above.