Skip to content

Commit 1b2c07a

Browse files
remyluslosiusclaude
andcommitted
fix: Resolve infinite re-render loop causing /scans page flickering
- Fix useEffect dependency array that was causing infinite loop - Use useRef to track scans state without triggering re-renders - Separate initial load from periodic refresh logic - Maintain periodic refresh functionality for running scans - Page now stable and readable without flickering The issue was on line 78 where useEffect had [scans] dependency, causing: useEffect → fetchScans() → setScans() → useEffect → infinite loop 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 6331355 commit 1b2c07a

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

frontend/src/pages/scans/Scans.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useEffect } from 'react';
1+
import React, { useState, useEffect, useRef } from 'react';
22
import {
33
Box,
44
Button,
@@ -41,13 +41,16 @@ const Scans: React.FC = () => {
4141
const [scans, setScans] = useState<Scan[]>([]);
4242
const [loading, setLoading] = useState(false);
4343
const [error, setError] = useState<string | null>(null);
44+
const scansRef = useRef<Scan[]>([]);
4445

4546
const fetchScans = async () => {
4647
try {
4748
setLoading(true);
4849
setError(null);
4950
const data = await api.get<{scans: Scan[]}>('/api/scans/');
50-
setScans(data.scans || []);
51+
const newScans = data.scans || [];
52+
setScans(newScans);
53+
scansRef.current = newScans;
5154
} catch (error: any) {
5255
console.error('Failed to load scans:', error);
5356

@@ -67,15 +70,15 @@ const Scans: React.FC = () => {
6770
useEffect(() => {
6871
fetchScans();
6972

70-
// Set up periodic refresh for running scans
73+
// Set up periodic refresh for running scans using ref to avoid infinite loop
7174
const interval = setInterval(() => {
72-
if (scans.some(scan => scan.status === 'running')) {
75+
if (scansRef.current.some(scan => scan.status === 'running')) {
7376
fetchScans();
7477
}
7578
}, 10000); // Refresh every 10 seconds if there are running scans
7679

7780
return () => clearInterval(interval);
78-
}, [scans]);
81+
}, []); // Empty dependency array - only run once
7982

8083
const getStatusColor = (status: string) => {
8184
switch (status) {

0 commit comments

Comments
 (0)