Next.js Discord

Discord Forum

useEffect not working unless I refresh the page using `cmd + R`

Unanswered
Pteromalid wasp posted this in #help-forum
Open in Discord
Pteromalid waspOP
Hi,
I have a working and running application using "next": "13.4.10" with the app router and deployed with vercel using node 18.x.x.
I'm trying to animate a simple text using the useEffect hook in a client component like this:

"use client";

import { useEffect, useState } from "react";

export default function AnimatedText() {
  const [state, setState] = useState<string>("logo");

  useEffect(() => {
    console.log("Trigger animation");
    const animateText = setTimeout(() => {
      setState("text");
    }, 2250);
    const animateExit = setTimeout(() => {
      setState("exit");
    }, 4500);
    return () => {
      clearTimeout(animateText);
      clearTimeout(animateExit);
    };
  }, []);

  return <p>{state}</p>
}

This component is imported inside the app/page.tsx.
So, if understand well, I should have a text that update it's state when the component is mounted and displayed as a paragraph inside my page.

When my link is deployed on Vercel and when I open the link in a new tab, nothing happen.
I have to refresh the page so the inside of the useEffect is working properly.

Is anyone have an idea?
Thanks!

15 Replies

@aardani Have you tried updating to next.js?
Pteromalid waspOP
Yes, still not working as expected
Do you have a demo?
Pteromalid waspOP
Unfortuntlay it's a private repo, but I have deployed the website
demo means you reproduce the bug on a separate repo
Pteromalid waspOP
@aardani demo means you reproduce the bug on a separate repo
Pteromalid waspOP
Ah, no
I can share my screen, if you want
well its to be expected for a bug to have a minimal reproduction repository
Pteromalid waspOP
Ok, I understand. I didin't have the time yet to reproduce it
it works here
something wrong on your end then
Pteromalid waspOP
Thanks, so strange
I have indented client component, I'm trying console.log in each on of them to see if one at the top is not rendered