[React] Under React 19 `<Activity>`, the now timer keeps a destroyed calendar alive, and a re-shown grid jumps back to its start
Versions
@fullcalendar/react7.1.0, with its bundledtimegridanddaygridplugins. Both bugs are in this package.@fullcalendar/react-scheduler7.1.0's resource time grid uses the sameTimeGridLayout, so it shows the scroll bug too.react/react-dom19.3.0 (<Activity>is stable since 19.2),temporal-polyfill1.0.5.- Chromium 153. The timer bug does not depend on the browser.
Background
<Activity mode="hidden"> keeps a subtree mounted but hidden (display: none). Frameworks use it to keep
recently visited routes alive; for example, Next.js 16 keeps up to three routes mounted this way. When a subtree is
hidden, React runs its effect cleanups and class componentWillUnmount, and it keeps re-rendering the subtree at
low priority. When the subtree is shown again, React runs its effects and componentDidMount again, on the same
instances. When a hidden subtree is finally removed, no cleanup runs, because it already ran when it was hidden.
Bug 1: the now timer re-arms after it was destroyed, and keeps the destroyed calendar alive
NowTimerRunner.update() arms its timer (a setTimeout that re-arms itself) and adds the nowManager reset
listener and a visibilitychange listener. It is called from render(): by NowTimer.render(), and by
CalendarDataManager's reducer, which the Calendar component runs from its render.
NowTimerRunner.destroy() runs from componentWillUnmount and from the Calendar component's effect cleanup.
Under <Activity>:
- Hiding runs
destroy(), which clears the timer and listeners. - Any re-render of the hidden calendar calls
update(). In the reproduction below, the parent's re-render that hides it is already one, because it passes a newpluginsarray. The runner is no longer marked as mounted, so it arms everything again. - When the hidden calendar is removed, nothing runs
destroy()again. The timer keeps re-arming itself every minute (or every day, for the calendar's own day timer), forever.
The pending timer's callback holds the runner, and the runner holds the calendar through handleChange. So
a heap snapshot taken after the calendar is gone still shows the whole calendar alive, retained by:
Window → DOMTimerCoordinator → DOMTimer → ScheduledAction → the runner's timeout callback →
NowTimerRunner → CalendarDataManager → the calendar's options, events and view state.
An app that shows and hides a calendar route leaks one full calendar per visit. We counted 13
CalendarDataManager objects alive after 12 visits.
The same root cause, side effects in render(), also hits a render that React throws away before it commits
(Suspense, concurrent rendering). That render arms a timer nothing clears, and a minute later it calls
forceUpdate on a component that never mounted. React then logs "Can't perform a React state update on a
component that hasn't mounted yet".
Bug 2: a grid shown again jumps back to its start
TimeGridLayout.componentDidMount calls resetScroll(), which scrolls to scrollTime.
DayGridLayout.componentDidMount does the same, scrolling to the current date's row. <Activity> runs
componentDidMount again on the same instance each time the calendar is shown. So a user who scrolled the grid,
left and came back finds it scrolled to scrollTime (time grid) or to today's row (day grid). Everything else
about the grid is kept.
Reproduction
Plain React and FullCalendar only. Any React 19.2+ setup works; this used Vite with the automatic JSX runtime.
The page counts FullCalendar's pending timers by wrapping window.setTimeout and counting only timers of 1 s or
more. FullCalendar reads the global at call time.
index.html:
<!doctype html>
<html>
<body>
<div id="root"></div>
<script type="module" src="./main.jsx"></script>
</body>
</html>main.jsx:
// FullCalendar 7.1.0 under React 19 <Activity>: the now timer outlives the calendar, and a grid shown
// again jumps back to its start. Load the page, then use the buttons (steps in the issue).
import FullCalendar from "@fullcalendar/react";
import dayGridPlugin from "@fullcalendar/react/daygrid";
import timeGridPlugin from "@fullcalendar/react/timegrid";
import { Activity, useEffect, useState } from "react";
import { createRoot } from "react-dom/client";
import "@fullcalendar/react/skeleton.css";
// Count FullCalendar's now timers: every pending setTimeout of 1 s or more. FullCalendar calls the global at
// call time; React schedules through MessageChannel.
const live = new Set();
const realSetTimeout = window.setTimeout;
const realClearTimeout = window.clearTimeout;
window.setTimeout = (callback, ms = 0, ...args) => {
const id = realSetTimeout(() => {
live.delete(id);
callback(...args);
}, ms);
if (ms >= 1000) live.add(id);
return id;
};
window.clearTimeout = (id) => {
live.delete(id);
realClearTimeout(id);
};
window.liveTimers = () => live.size;
// "timeGridWeek" shows the time grid's reset; "dayGridMonth" (in a short calendar) the day grid's.
const VIEW = new URLSearchParams(location.search).get("view") ?? "timeGridWeek";
// Enough all-day events on today that a short month grid has to scroll.
const EVENTS = Array.from({ length: 12 }, (_, i) => ({ title: `Event ${i + 1}`, start: new Date(), allDay: true }));
function App() {
const [isMounted, setIsMounted] = useState(true);
const [isShown, setIsShown] = useState(true);
const [, setRevision] = useState(0);
const [timers, setTimers] = useState(0);
useEffect(() => {
const id = setInterval(() => setTimers(window.liveTimers()), 200);
return () => clearInterval(id);
}, []);
return (
<>
<p>
<button type="button" onClick={() => setIsShown(false)}>Hide</button>{" "}
<button type="button" onClick={() => setRevision((n) => n + 1)}>Re-render</button>{/* re-renders the hidden calendar */}{" "}
<button type="button" onClick={() => setIsShown(true)}>Show</button>{" "}
<button type="button" onClick={() => setIsMounted(false)}>Unmount</button>{" "}
<output id="timers">now timers pending: {timers}</output>
</p>
{isMounted && (
<Activity mode={isShown ? "visible" : "hidden"}>
<FullCalendar
plugins={[timeGridPlugin, dayGridPlugin]}
initialView={VIEW}
nowIndicator
height={VIEW === "dayGridMonth" ? 300 : 500}
scrollTime="06:00"
events={EVENTS}
/>
</Activity>
)}
</>
);
}
createRoot(document.getElementById("root")).render(<App />);Bug 1 steps (open /, the week view)
- Load the page. The counter shows 2 pending timers: the calendar's day timer and the now indicator's.
- Click Hide, then Re-render.
- Click Unmount.
| After | Expected | Actual (7.1.0) |
|---|---|---|
| Hide | 0 | 1 |
| Re-render | 0 | 1 |
| Show | 2 | 1 |
| Unmount | 0 | 1, for good |
After Unmount, a heap snapshot (DevTools, or CDP HeapProfiler.takeHeapSnapshot after
HeapProfiler.collectGarbage) still contains one CalendarDataManager. It is the only object with a
currentCalendarOptionsInput property. The expected count is 0.
Bug 2 steps
- Load
/(week view,scrollTime06:00). Scroll the time grid with the mouse wheel. - Click Hide, then Show.
- Load
/?view=dayGridMonth(a 300 px calendar with 12 events on today, so its grid scrolls) and repeat.
| View | Scrolled to | Expected after Show | Actual (7.1.0) |
|---|---|---|---|
timeGridWeek |
629 px | 629 px | 229 px (back at scrollTime) |
dayGridMonth |
0 px | 0 px | 42 px (back at today's row) |
We measured both tables with Playwright in Chromium 153, before and after the change below. With it: 0 timers after
Hide and after Unmount, 2 after Show, 0 CalendarDataManager after Unmount, and both grids keep their scroll.
Suggested fix
- Arm the runner on mount, not in
update(). AddNowTimerRunner.mount(), which adds the listeners and runshandleRefresh(), which sets the timer. Call it fromNowTimer.componentDidMountand from theCalendarcomponent's mount effect, through a newCalendarDataManager.mount().update()then only keeps the inputs and outputs current, and re-arms the timer only when the runner is mounted. A render that never mounts, or that runs while hidden, arms nothing. - Reset the scroll only on the first mount. On a later mount of the same instance,
TimeGridLayoutcallsapplyTimeScroll(), which puts back the last scroll the user made.DayGridLayoutleaves the scroll alone: the browser restores an element'sscrollTopwhen itsdisplay: noneends.
The same change as a patch against the published 7.1.0 dist (pnpm patch format). The chunk file names are
the build's; each hunk header names the class it changes.
diff --git a/chunks/4fcf1299.js b/chunks/4fcf1299.js
index 84f8f5ce726aec52f4467ad34ecd05d4b0c09aac..fb4ce2dd78b461efe71296d1e199ff91eb3ed151 100644
--- a/chunks/4fcf1299.js
+++ b/chunks/4fcf1299.js
@@ -282,7 +282,11 @@ class DayGridLayout extends BaseComponent {
componentDidMount() {
this._isUnmounting = false;
if (!this.props.forPrint) {
- this.resetScroll();
+ // A React <Activity> shown again mounts the same instance again: keep the scroll the browser kept.
+ if (!this.hasMounted) {
+ this.hasMounted = true;
+ this.resetScroll();
+ }
this.scrollerRef.current?.addScrollEndListener(this.handleScrollEnd);
}
}
diff --git a/chunks/7c9b34a3.js b/chunks/7c9b34a3.js
index d181c9140c106958ecd75c4d6a710d5a790dcea2..7c5015d91c5f9d40e60f6a7b301cc6824b1db58f 100644
--- a/chunks/7c9b34a3.js
+++ b/chunks/7c9b34a3.js
@@ -18,6 +18,7 @@ const Calendar = forwardRef((props, ref) => {
onDataChange: handleDataChange,
}));
useEffect(() => {
+ calendarDataManager.mount();
return () => {
calendarDataManager.destroy();
};
diff --git a/chunks/8563f14c.js b/chunks/8563f14c.js
index 449b5581daf9291a5c5de3e81e4a5449d45d0644..8ed76c627ec88d0129784b14919637bd5ddacd3a 100644
--- a/chunks/8563f14c.js
+++ b/chunks/8563f14c.js
@@ -362,8 +362,7 @@ class NowTimerRunner {
};
}
update(input) {
- if (!this.isMounted) {
- this.isMounted = true;
+ if (!this.nowManager) {
// init inputs
this.unit = input.unit;
this.unitValue = input.unitValue;
@@ -375,14 +374,6 @@ class NowTimerRunner {
this.nowDate = timing.nowDate;
this.nowMs = timing.nowMs;
this.todayRange = timing.todayRange;
- // init listeners
- this.setTimeout(timing.waitMs);
- this.nowManager.addResetListener(this.handleRefresh);
- // fired tab becomes visible after being hidden
- // SSR check. CalendarDataManager calls top-level sync :(
- if (typeof document !== 'undefined') {
- document.addEventListener('visibilitychange', this.handleVisibilityChange);
- }
}
else if (input.unit !== this.unit ||
input.unitValue !== this.unitValue ||
@@ -402,8 +393,10 @@ class NowTimerRunner {
this.nowDate = timing.nowDate;
this.nowMs = timing.nowMs;
this.todayRange = timing.todayRange;
- this.clearTimeout();
- this.setTimeout(timing.waitMs);
+ if (this.isMounted) {
+ this.clearTimeout();
+ this.setTimeout(timing.waitMs);
+ }
}
return {
nowDate: this.nowDate,
@@ -411,6 +404,17 @@ class NowTimerRunner {
todayRange: this.todayRange,
};
}
+ // Arms the timer and listeners on mount, not in update(): a render that never mounts (discarded, or
+ // re-run while hidden in a React <Activity> after unmount) would leave them running with nothing to clear them.
+ mount() {
+ if (!this.isMounted) {
+ this.isMounted = true;
+ this.nowManager.addResetListener(this.handleRefresh);
+ // fired tab becomes visible after being hidden
+ document.addEventListener('visibilitychange', this.handleVisibilityChange);
+ this.handleRefresh();
+ }
+ }
destroy() {
if (this.isMounted) {
this.isMounted = false;
diff --git a/chunks/af2b51d5.js b/chunks/af2b51d5.js
index 8af8cdeff5236211f8c8c58d91eb6c7ad7f54a1d..219ff0c95ee449f4eda6200d6745e0069a45a6d9 100644
--- a/chunks/af2b51d5.js
+++ b/chunks/af2b51d5.js
@@ -773,6 +773,9 @@ class CalendarDataManager {
this.nowManager = new CalendarNowManager();
this.nowTimer = new NowTimerRunner(this.handleNowChange);
}
+ mount() {
+ this.nowTimer.mount();
+ }
destroy() {
this.nowTimer.destroy();
}
diff --git a/chunks/bdcb9f0b.js b/chunks/bdcb9f0b.js
index 17c2f693a3200f7f1a390b9f1f629e7e98a8c940..5f9b0efffe309c1b733e5792d20bed779c2cd67f 100644
--- a/chunks/bdcb9f0b.js
+++ b/chunks/bdcb9f0b.js
@@ -27,6 +27,9 @@ class NowTimer extends Component {
});
return props.children(nowDate, todayRange, nowMs);
}
+ componentDidMount() {
+ this.runner.mount();
+ }
componentWillUnmount() {
this.runner.destroy();
}
diff --git a/chunks/fcbe493e.js b/chunks/fcbe493e.js
index efca54ca9b458bdbb3fd68638516446c7fc6436e..d5226129d6be3100119ba1f7f08442a3912cc5b2 100644
--- a/chunks/fcbe493e.js
+++ b/chunks/fcbe493e.js
@@ -1763,7 +1763,14 @@ class TimeGridLayout extends BaseComponent {
// -----------------------------------------------------------------------------------------------
componentDidMount() {
this._isUnmounting = false;
- this.resetScroll();
+ // A React <Activity> shown again mounts the same instance again: keep the scroll it was left at.
+ if (this.hasMounted) {
+ this.applyTimeScroll();
+ }
+ else {
+ this.hasMounted = true;
+ this.resetScroll();
+ }
this.context.emitter.on('_timeScrollRequest', this.handleTimeScrollRequest);
const timeScroller = this.timeScrollerRef.current;
if (timeScroller) {Source: fullcalendar/fullcalendar