Next.js Discord

Discord Forum

UseEffect Never Executes

Answered
Atlantic menhaden posted this in #help-forum
Open in Discord
Atlantic menhadenOP
Currently for NextJS 13 app file structure is like:
├── app
|   ├── components
|      └── Dashboard.tsx
│   ├── dashboard
│      └── pages.tsx


My main issue is I cannot get my UseEffect to fire off as it currently is. Not sure why not because I have looked a lot online and found this way very common. my Dashboard component logs "Hello World" and renders the h1 but does not execute the useEffect hook.

Dashboard.tsx
'use client'

import React from 'react'


export default function Dashboard() {

    console.log("Hello World")

    useEffect(() => {
        console.log("useEffect")
    }, []);

    return (
        <>
            <h1>Tickers</h1>
        </>
    )
}


dashboard/page.tsx
import React from 'react'
import Dashboard from '../components/Dashboard'

export default function DashboardPage() {

    return (
        <>
            <h1>Dashboard</h1>
            <Dashboard />
        </>
    )
}


UPDATE:

I started a fresh NextJS Project and tried doing the same dashboard route and the useEffect still would not fire off for some reason.
Answered by Atlantic menhaden
my version of node is v16.13.1 and the requirement for nextjs 13 is 16.14.0 wow this might be it
View full answer

42 Replies

Asian black bear
You are looking for the useEffect message in the browser console right?
@Asian black bear You are looking for the `useEffect` message in the browser console right?
Atlantic menhadenOP
yes I get nothing displayed
Atlantic menhadenOP
screenshot of what happens after I hit the page where the useEffect should render
Asian black bear
Yeah I don't know then. The only weird thing is putting components inside of /app, usually people have it outside
Another thing to try would be to stop the dev server and try deleting the .next folder
then doing a reload
@Asian black bear Yeah I don't know then. The only weird thing is putting `components` inside of `/app`, usually people have it outside
Atlantic menhadenOP
didnt know that (nextjs noob) but tried moving it with imports adjusted and no difference
also I did already try deleting the .next
no difference
Asian black bear
what is weird is that the "hello world" should also log in the browser console
in fact it should log several times
Atlantic menhadenOP
hmm yeah I was thinking if the useEffect should log in console too hello world should as well, weird that probably has something to do with my issue
Asian black bear
personally, I would try changing the text in <Dashboard> to see if it changes
my suspicion is that (somehow) the code that you have shown is not actually running
@Asian black bear personally, I would try changing the text in `<Dashboard>` to see if it changes
Atlantic menhadenOP
Tried that no difference
Asian black bear
did the text change in the rendered page?
Atlantic menhadenOP
yes but still no console log in browser
Asian black bear
I mean, it is kind of slightly impossible for that to happen without logging "hello world"
Atlantic menhadenOP
if it is printing to the terminal and not the browser console like it should perhaps it is an issue is with SSR? I have the 'use client'; directive so it should be not be in terminal
@Asian black bear I mean, it is kind of *slightly* impossible for that to happen without logging "hello world"
Atlantic menhadenOP
yeah it logs it in the terminal
though
Asian black bear
So, my next suspicion is that your browser is not showing your messages. Can you try to use a different browser to load localhost:3000 like edge?
the filter stuff in your screenshot appears to be correct, but idk, sometimes chrome devtools do wonky stuff
Atlantic menhadenOP
just tried firefox and still no output
Asian black bear
An alternative experiment

export default function Dashboard() {
    const [n,setN]=useState('hello');
    console.log("Hello World")

    useEffect(() => {
        console.log("useEffect")
        setN('world')
    }, []);

    return (
        <>
            <h1>Tickers ({n})</h1>
        </>
    )
}
Atlantic menhadenOP
no broswer output
reloaded app -> went to /dashboard to render it
Asian black bear
ok, so it really is not hydrating
which is fascinating and weird
are these the only things in your project, or is there other stuff?
if the latter, I would make a new next project with literally only these two files (where it WILL work) and slowly add the other stuff until something breaks!
@Asian black bear if the latter, I would make a new next project with literally only these two files (where it WILL work) and slowly add the other stuff until something breaks!
Atlantic menhadenOP
I just made a new nextjs project, added just those 2 files, and still no console log, wooow maybe my Node is out of date or something hmm I think this means theres something else wrong
Asian black bear
It is weird because stuff is updating, so nothing is stale
Then you tried opening a new browser so local browser state probably isn't it
Atlantic menhadenOP
my version of node is v16.13.1 and the requirement for nextjs 13 is 16.14.0 wow this might be it
Answer
Asian black bear
oh my
are you using VSCode remote?
Atlantic menhadenOP
no im on my macbook, gonna update and will try again
Atlantic menhadenOP
@Asian black bear node version update fixed it 🥲
do you have a buy you a coffee link for the help?
@Atlantic menhaden do you have a `buy you a coffee` link for the help?
Asian black bear
I wish I could have helped more, not hydrating because of the node version was extremely surprising!