react-datepicker
react-datepicker copied to clipboard
Unnecessary code when calling event handler props
Throughout index.tsx there are many instances of code similar to the below:
onChange
? onChange(changedDate, event)
: DatePicker.defaultProps.onChange;
This is problematic for several reasons:
- The 'else' line of the ternary does nothing.
DatePicker.defaultProps.onChangereferences a function, but the function is not called as the brackets()are missing. - Even if we did add the brackets,
DatePicker.defaultProps.onChangeis an empty function, so calling it achieves nothing. - The code is therefore equivalent to just
onChange && onChange(changedDate, event)which can be more simply writtenonChange?.(changedDate, event) - Providing an empty function, as opposed to undefined, as the default prop, does not appear useful, as the calling code always checks that the prop is truthy (not undefined) before calling it. We are forced to check the function is not undefined (rather than relying on the default prop always being defined) as the user can pass
nullas the event handler prop, and we do not want to fail in that case. - With the above code change, we no longer reference the default prop. Also, calling an empty function will never have a different effect than doing nothing at all.
So it would seem reasonable to refactor these 3 lines of code into one in the many places where they appear, and to remove the default prop empty functions as they are not needed.
This refactoring will then make it easier to implement further changes, such has correctly handling the input of date ranges in the textbox, as requested in issues #3596, #3906, #4002.