From a9f42acbf6ac7fd5d63814d3047c28048d308779 Mon Sep 17 00:00:00 2001 From: Usman Baig Date: Tue, 10 Mar 2026 01:03:12 +0100 Subject: [PATCH] Use ref for lastUpdatedAt to avoid extra re-render on mount MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When navigating to dashboard with cached SWR data, setLastUpdatedAt triggered a second full render of the entire dashboard immediately after the first. Using a ref instead avoids this — the value still updates and Chart reads it on the next 1-second tick re-render. Co-Authored-By: Claude Opus 4.6 --- app/sites/[id]/page.tsx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/sites/[id]/page.tsx b/app/sites/[id]/page.tsx index cee7ca5..d4ba1b9 100644 --- a/app/sites/[id]/page.tsx +++ b/app/sites/[id]/page.tsx @@ -2,7 +2,7 @@ import { logger } from '@/lib/utils/logger' -import { useCallback, useEffect, useState, useMemo } from 'react' +import { useCallback, useEffect, useRef, useState, useMemo } from 'react' import { useParams, useRouter, useSearchParams } from 'next/navigation' import { getPerformanceByPage, @@ -99,7 +99,7 @@ export default function SiteDashboardPage() { ) const [isDatePickerOpen, setIsDatePickerOpen] = useState(false) const [isExportModalOpen, setIsExportModalOpen] = useState(false) - const [lastUpdatedAt, setLastUpdatedAt] = useState(null) + const lastUpdatedAtRef = useRef(null) const [, setTick] = useState(0) // Dimension filters state @@ -371,7 +371,7 @@ export default function SiteDashboardPage() { // Track when data was last updated (for "Live · Xs ago" display) useEffect(() => { - if (overview) setLastUpdatedAt(Date.now()) + if (overview) lastUpdatedAtRef.current = Date.now() }, [overview]) // Tick every 1s so "Live · Xs ago" counts in real time @@ -537,7 +537,7 @@ export default function SiteDashboardPage() { setTodayInterval={setTodayInterval} multiDayInterval={multiDayInterval} setMultiDayInterval={setMultiDayInterval} - lastUpdatedAt={lastUpdatedAt} + lastUpdatedAt={lastUpdatedAtRef.current} annotations={annotations} canManageAnnotations={true} onCreateAnnotation={handleCreateAnnotation}