diff --git a/components/carnet/health-form.tsx b/components/carnet/health-form.tsx index 7454ba6..a51de1c 100644 --- a/components/carnet/health-form.tsx +++ b/components/carnet/health-form.tsx @@ -1,6 +1,6 @@ "use client"; -import React, { useState, useEffect } from 'react'; +import React, { useState, useEffect, useRef } from 'react'; import { Calendar, Activity, Heart, Pill, Droplet, Dumbbell, FileText } from 'lucide-react'; interface HealthData { @@ -56,6 +56,9 @@ export const HealthForm: React.FC = ({ content, onContentChange return {}; }); + // Use ref to track last sent content to prevent infinite loops + const lastSentContentRef = useRef(''); + // Initialize date if not set useEffect(() => { if (!data.date && date) { @@ -69,14 +72,15 @@ export const HealthForm: React.FC = ({ content, onContentChange // Only update if content actually changed to prevent infinite loops useEffect(() => { const jsonContent = JSON.stringify(data, null, 2); - // Only update if content is different from current content - if (jsonContent !== content) { + // Only update if content is different from last sent content + if (jsonContent !== lastSentContentRef.current) { const timer = setTimeout(() => { + lastSentContentRef.current = jsonContent; onContentChange(jsonContent); }, 500); // Increased delay to reduce updates return () => clearTimeout(timer); } - }, [data]); // Removed onContentChange and content from dependencies to prevent loops + }, [data, onContentChange]); // Stable dependencies const updateField = (field: keyof HealthData, value: any) => { setData(prev => ({ ...prev, [field]: value }));