react-native-calendar-strip icon indicating copy to clipboard operation
react-native-calendar-strip copied to clipboard

TypeError: undefined is not an object (evaluating 'datesList[_this.state.numVisibleDays - 1].date')

Open gustavomanolo opened this issue 3 years ago • 11 comments

HI, i've added a calendar strip instance like this:

<CalendarStrip style={{ height: 100, paddingTop: 10, paddingBottom: 10 }} minDate={moment()} maxDate={moment().add(10, "days")} datesBlacklist={(date) => { return (date.isoWeekday() === 6 || date.isoWeekday() === 7); }} />

But i'm getting an error: TypeError: undefined is not an object (evaluating 'datesList[_this.state.numVisibleDays - 1].date')

If i remove the "datesBlacklist" property, it works but i need to disable saturdays and sundays.

I'm using version => "react-native-calendar-strip": "^2.2.3"

Thanks in advance.

gustavomanolo avatar Jun 14 '21 22:06 gustavomanolo

Are you passing in another other props? Does your code work with the sample app? https://github.com/BugiDev/react-native-calendar-strip#development-with-sample-application

peacechen avatar Jun 14 '21 23:06 peacechen

This is a dup of #262. This error occurs when componentDidUpdate is called before onLayout is called. onLayout is where the numVisibleDays state is set. I am not using scrollable. I can get this error to happen 100% of the time by showing a screen with CalendarStrip, going to another screen, and coming back to the screen with CalendarStrip. If I don't use datesWhitelist then the error does not occur.

phatmann avatar Jun 20 '21 05:06 phatmann

More details: if the datesWhitelist property is changed several times very quickly, then componentDidUpdate ends up firing before onLayout is called. In my code, the bug happens when it changes 3 times in a row. Maybe reasonable defaults for all the state properties can be set before the measurements are done.

phatmann avatar Jun 20 '21 06:06 phatmann

Here is a possible fix: stop componentDidUpdate from being called until layout is done. I places these lines at beginning of componentDidUpdate and the problem went away:

    if (!this.state.dayComponentWidth) {
      return
    }

I am not sure if this will cause other problems or not, which is why I will not submit a PR with this fix yet.

phatmann avatar Jun 20 '21 07:06 phatmann

@phatmann I made a PR out of your suggested fix, which also worked for me. https://github.com/BugiDev/react-native-calendar-strip/pull/314

mtimofiiv avatar Jul 13 '21 07:07 mtimofiiv

Thanks @mtimofiiv and @phatmann for identifying the issue.

That change will likely cause undesirable side effects, and is a band-aid for the root problem. Please revert that change in the PR and instead add this to the state initializer: https://github.com/BugiDev/react-native-calendar-strip/blob/master/src/CalendarStrip.js#L142

    this.state = {
      startingDate,
      selectedDate,
      datesList: [],
      dayComponentWidth: 0,
      height: 0,
      monthFontSize: 0,
      selectorSize: 0,
      numVisibleDays: props.numDaysInWeek,
    };

peacechen avatar Jul 13 '21 14:07 peacechen

@peacechen you're right. I've updated the PR by reverting the old change and adding the initial state as you suggested.

mtimofiiv avatar Jul 14 '21 03:07 mtimofiiv

Thanks again @mtimofiiv for updating the PR. The fix has been published in 2.2.4

peacechen avatar Jul 14 '21 04:07 peacechen

Hmm, the fix has not seemed to work, still getting the same error on 2.2.4. I can look into this a bit further later.

mtimofiiv avatar Jul 21 '21 00:07 mtimofiiv

This fix caused other issues for me. I decided not to use it. I can look into it if needed.

phatmann avatar Jul 22 '21 21:07 phatmann

In 2.2.5 I have not this problem anymore

Marcoo09 avatar Oct 18 '21 23:10 Marcoo09