react-datepicker: v2.20.0 of date-fns seems to break react-datepicker
Describe the bug We upgraded to v2.20.0 of date-fns which causes “TypeError: Cannot read property ‘locale’ of null”. Hard to trace what actually caused this issue in date-fns - the offending call is this:
getWeek(e,r ? { locale : r } : null)
It looks as though prior to v2.20.0, the second arg to getWeek did not have a default value and now it defaults to {}. Since null is being passed here, that won’t use the default value and further down the call stack in getWeek(), it calls getWeekYear() which assumes that options will be defaulted to {} which then causes the error.
TypeError: Cannot read property 'locale' of null
10:03:42 at getWeekYear (../node_modules/date-fns/getWeekYear/index.js:66:24)
10:03:42 at startOfWeekYear (../node_modules/date-fns/startOfWeekYear/index.js:68:33)
10:03:42 at getWeek (../node_modules/date-fns/getWeek/index.js:65:81)
10:03:42 at Se (../node_modules/react-datepicker/dist/index.js:1:6776)
10:03:42 at n.formatWeekNumber (../node_modules/react-datepicker/dist/index.js:1:27625)
10:03:42 at n.renderDays (../node_modules/react-datepicker/dist/index.js:1:27729)
10:03:42 at n.value (../node_modules/react-datepicker/dist/index.js:1:29083)
To Reproduce Steps to reproduce the behavior:
Seems to be happening when we show the date picker. Worth mentioning that this is happening in our tests (causing failures) which uses react-testing-library and js-dom
Expected behavior No TypeErrors should occur
- OS: Windows
- Browser js-dom
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 14
- Comments: 21 (4 by maintainers)
Commits related to this issue
- Fix default argument value usage Fixed a breaking change introduced by using modern default argument value syntax (see https://github.com/Hacker0x01/react-datepicker/issues/2870). — committed to date-fns/date-fns by kossnocorp 3 years ago
- Fix default argument value usage (#2423) Fixed a breaking change introduced by using modern default argument value syntax (see https://github.com/Hacker0x01/react-datepicker/issues/2870). — committed to date-fns/date-fns by kossnocorp 3 years ago
- Auto merge of #3524 - rust-lang:renovate/date-fns-2.x, r=Turbo87 Update dependency date-fns to v2.21.1 [](https://renovatebot.co... — committed to rust-lang/crates.io by bors 3 years ago
Boy, aren’t the 739,604 who downloaded and installed it this week in for a surprise when their production apps all hard crash the page when someone focuses on a date picker field…
Faced with the same issue, the solution works for me - has been added locale for DatePicker component. E. g.
We encountered the same issue today. "v2.20.0 of date-fns seems to break react-datepicker ". it would crash the whole page.
Can confirm, after removing
node_modules,package-lock.jsonand doing a cleannpm install, version 2.20.0 of date-fns gets installed and we can reproduce this crashWe are facing the exact same issue. Please provide any temporary fix till we have a permanent solution. Thanks.
Can confirm
date-fns@v2.21.1has fixed this for us. Thanks @kossnocorp!We’ve shipped
date-fns@v2.21.1with a fix for this problem. We’ve rolled back usage of the default argument value syntax for options. We will reintroduce that in v3, but as a breaking change this time. Sorry for the trouble, everyone!It looks like there is another break related to passing an existing date to the “selected” parameter as a string in the DatePicker component:
Starting with v2.0.0-beta.1 date-fns doesn't accept strings as date arguments. Please use "parseISO" to parse strings. See: https://git.io/fjuledate-fns getWeekOfYear has changed to use TypeScript and now its options var is set up like this:
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};whereas before 2.20 it was this:
var options = dirtyOptions || {};So since datepicker is passing null when locale is absent, the second argument isn’t undefined and
var locale = options.locale;in getWeekOfYear with null options is causing the TypeError.Perhaps date-fns should be encouraged to add the truthy check back in?
With
"date-fns": "^1.30.1"and"react-datepicker": "^2.0.0", the following worked for me: