1- import React , { useState , useEffect } from "react" ;
1+ import React , { useState , useEffect , useCallback } from "react" ;
22import { NavLink , Link , useNavigate , useLocation } from "react-router-dom" ;
33import { FaMoon , FaSun , FaUserCircle , FaBell } from "react-icons/fa" ;
44import "./Header.css" ;
@@ -18,7 +18,7 @@ const Header = ({ isDark, setIsDark, isLoggedIn, nickname, onLoginModalOpen }) =
1818 const navigate = useNavigate ( ) ;
1919 const location = useLocation ( ) ;
2020
21- const fetchNotifications = async ( ) => {
21+ const fetchNotifications = useCallback ( async ( ) => {
2222 if ( ! isLoggedIn ) return ;
2323 try {
2424 const token = localStorage . getItem ( 'token' ) ;
@@ -44,19 +44,16 @@ const Header = ({ isDark, setIsDark, isLoggedIn, nickname, onLoginModalOpen }) =
4444 } catch ( error ) {
4545 console . error ( "Error fetching notifications:" , error ) ;
4646 }
47- } ;
47+ } , [ isLoggedIn ] ) ;
4848
4949 useEffect ( ( ) => {
5050 if ( isLoggedIn ) {
5151 fetchNotifications ( ) ;
52-
53- const intervalId = setInterval ( ( ) => {
54- fetchNotifications ( ) ;
55- } , 10000 ) ;
56-
57- return ( ) => clearInterval ( intervalId ) ;
52+ } else {
53+ setNotifications ( [ ] ) ;
54+ setUnreadCount ( 0 ) ;
5855 }
59- } , [ isLoggedIn ] ) ;
56+ } , [ isLoggedIn , fetchNotifications ] ) ;
6057
6158 const toggleNotificationMenu = ( ) => {
6259 setIsNotificationMenuOpen ( ! isNotificationMenuOpen ) ;
0 commit comments