My useEffect is not firing at all:
import React, { useEffect } from 'react'
function Home(props) {
useEffect = (() => {
onPageLoad()
}, [])
const onPageLoad = () => {
console.log('hello from page load')
}
return (
<p>Home Component</p>
)
}
export default Home
I’m stumped on this. There’s nothing in the console.
4
Answers
There should be something some error at least in the developer console in the browser, since it seems you have written a syntax error. You are currently trying to assign a new value to the
import
ed variableuseEffect
but I believe you may have meant to call the function instead:UPDATE
Just noticed this, your
useEffect
function is read-only. Notice the=
sign afteruseEffect
. Hence, the function might not be getting called.Instead, can you please try something like mentioned below.
Also, I suggest try declaring the
onPageLoad
function outside of thecomponent
.There is a syntax error (
Error: "useEffect" is read-only
) in your code. Please have a look at more details about the Error: useEffect read-only.It should be like this:
Instead of this:
Please have a look at the working CodeSandbox example.
if you want fire
onPageLoad
for each navigate useloc=useLocation()
andloc.path
as dependencies useEffect .also please declare and then use-it . one Clouse function is enough