Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

useMemo

Blue Mouse006b2b59e60a
diff --git a/web/app/modals/AnnotationModal.tsx b/web/app/modals/AnnotationModal.tsx
index f812e0e22..1ea2035b1 100644
--- a/web/app/modals/AnnotationModal.tsx
+++ b/web/app/modals/AnnotationModal.tsx
@@ -1,5 +1,5 @@
import { InfoIcon } from 'lucide-react'
-import { useState, useEffect } from 'react'
+import { useState, useEffect, useMemo } from 'react'
import { useTranslation } from 'react-i18next'
import { Annotation } from '~/lib/models/Project'
@@ -33,17 +33,20 @@ const AnnotationModal = ({
allowedToManage = true,
}: AnnotationModalProps) => {
const { t } = useTranslation('common')
- const [date, setDate] = useState(annotation?.date || defaultDate || '')
- const [text, setText] = useState(annotation?.text || '')
- useEffect(() => {
- if (!isOpened) {
- return
- }
+ const initialDate = useMemo(
+ () => (isOpened ? annotation?.date || defaultDate || '' : ''),
+ [isOpened, annotation?.date, defaultDate],
+ )
+ const initialText = useMemo(() => (isOpened ? annotation?.text || '' : ''), [isOpened, annotation?.text])
+
+ const [date, setDate] = useState(initialDate)
+ const [text, setText] = useState(initialText)
- setDate(annotation?.date || defaultDate || '')
- setText(annotation?.text || '')
- }, [isOpened, annotation, defaultDate])
+ useEffect(() => {
+ setDate(initialDate)
+ setText(initialText)
+ }, [initialDate, initialText])
const _onClose = () => {
setTimeout(() => {