question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

state value updates on calendar properties like "current" does not lead to the calendar re-rendering

See original GitHub issue

Please make our job easier by filling this template out to completion. If you’re requesting a feature instead of reporting a bug, please feel free to skip the Environment and Reproducible Demo sections.

Description

I am currently trying to use an external button to control the month shown on the calendar. For instance a “next” button would lead to the next month, a “prev” button would lead to the previous month. I am fully aware I can easily do that using the arrows but this is for UX/UI reasons.

I am attempting to realize this feature by putting the dates set on the “current” as a state and update them accordingly through the button utilizing the “useState” feature.

I.e.:

const [month, setMonth] = useState(“2021-03-24”);

return ( <View> <Calendar current={month} … > <TouchableOpacity onPress={() => { setMonth(“2021-04-24”); } …

When using the most recent version, the “setMonth” triggers a re-rendering of the screen except for the Calendar component. Thus, the calendar’s date never changes, although the “month” value changes appropriately.

I have searched this issue and it turns out there were similar complaints regarding the state updates not causing the Calendar components to re-render. It did say it was fixed in the 1.18.0 update 3 years ago so I tried downgrading it to that version and it does work appropriate on that version. The state changes properly triggers a re-rendering of the Calendar component. However, the downgrade causes different problems such as “Warning: componentWillReceiveProps has been renamed” errors due to it being an older version.

Can this please be fixed? It was fixed on the 1.18.0 version but somewhere along the newer versions, the problem was re-introduced to the library.

Expected Behavior

State changes properly triggers the re-rendering of the Calendar component.

Observed Behavior

Calendar component does not re-render.

Regarding the error message for the 1.18.0 version:

Warning: componentWillReceiveProps has been renamed, and is not recommended for use. See https://fb.me/react-unsafe-component-lifecycles for details.

* Move data fetching code or side effects to componentDidUpdate.
* If you're updating state whenever props change, refactor your code to use memoization techniques or move it to static getDerivedStateFromProps. Learn more at: https://fb.me/react-derived-state
* Rename componentWillReceiveProps to UNSAFE_componentWillReceiveProps to suppress this warning in non-strict mode. In React 17.x, only the UNSAFE_ name will work. To rename all deprecated lifecycles to their new names, you can run `npx react-codemod rename-unsafe-lifecycles` in your project source folder.

Please update the following components: %s, Calendar
- node_modules/react-native/Libraries/LogBox/LogBox.js:117:10 in registerWarning
- node_modules/react-native/Libraries/LogBox/LogBox.js:63:8 in warnImpl
- node_modules/react-native/Libraries/LogBox/LogBox.js:36:4 in console.warn
- node_modules/expo/build/environment/react-native-logs.fx.js:18:4 in warn
- node_modules/react-native/Libraries/Renderer/implementations/ReactNativeRenderer-dev.js:106:4 in printWarning

Environment

Please run these commands in the project folder and fill in their results:

  • npm ls react-native-calendars:
  • npm ls react-native:

Also specify:

  1. Phone/emulator/simulator & version: Android, Expo (bareflow exposed)

Reproducible Demo

Please provide a minimized reproducible demonstration of the problem you’re reporting.

Issues that come with minimal repro’s are resolved much more quickly than issues where a maintainer has to reproduce themselves.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:9

github_iconTop GitHub Comments

9reactions
gaultierqcommented, Apr 7, 2021

yep I have the same problem. My workaround is to use the ‘key’ prop :

<Calendar 
    key={current + ""} 
    current={current}  />

It will re-construct the all component.

1reaction
narradorwwcommented, Jan 28, 2022

🩹

const [currentMonth, setCurrentMonth] = useState(dateFormatter.format(new Date(), 'YYYY-MM-DD'));
const [calendarKey, setCalendarKey] = useState(0);

  const markToday = () => {
    const today = new Date();
    const stringToday = today.toISOString().slice(0, 10);
    onDayPress(stringToday);
    setCalendarKey(Math.random());
  };

<ThemedCalendar current={currentMonth} key={calendarKey} onMonthChange={onMonthChange} onDayPress={onDayPress} markedDates={markedDatesData} markingType={'multi-dot'} />

We had a similar issue. To fix it, we created a state for the key that would refresh the calendar, forcing it to render with the state of the key instead of current

Read more comments on GitHub >

github_iconTop Results From Across the Web

Updating State Object Does Not Rerender Dynamically ...
I have a component with three tabs in which I have unique filters for each tab. I am currently writing a Filter component...
Read more >
How to enable and disable the Outlook calendar sharing ...
This article provides details about how to turn on and turn off these shared calendar improvements in Outlook for Windows. Note: Shared Calendar...
Read more >
KendoReact Changelog - Telerik
page is scrolled as events are not prevented in tree components; label clicks cause the popup to bounce; toggleButton tabIndex; add missing key ......
Read more >
How to stop re-rendering lists in React? - Alex Sidorenko
First, let's simplify our example by removing all props from the Item . We will still update the parent state but won't pass...
Read more >
Exploring advanced customizations of react-table component ...
Now in table component we are passing tableitems state in the data ... Properties like _OnRowClick and _OnCellClick are not required ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found