1 year ago

#365952

test-img

Luciano Barrabino

State inside useEffect

I've no problems with the below code but I wanna be sure if there is no memory leak or something like that in this component.

const Nav = () => {
  const [activeNav,setActiveNav] = useState("");
  const  home = "#";
  
  const inTime = () => {
      setActiveNav("");
  };

  useEffect(() => {

    const timer = setTimeout(() => {
        inTime();
    },2000);

    return () => {
      clearTimeout(timer);
    };

  },[activeNav]);

  return (
    <nav>
      <a href={home} onClick={()=>setActiveNav(home)} className={activeNav===home?"active":""}><AiOutlineHome /></a>
    </nav>
  );
};

javascript

html

reactjs

settimeout

use-effect

0 Answers

Your Answer

Accepted video resources