#array dependancy on useEffectHook

9 messages · Page 1 of 1 (latest)

dark trail
#

Hey guys, should i include a dependancy array on this useEffect hook implementing google analytics? I havent got any issues at all but everytime a button is clicked that have event listeners for analytics the component re-renders( the console prints)

wraith dust
#

if you don't include dependency array, every time the component re-renders will execute your callback.

#

You would probably only want to do it on the first page load. An empty dependency list would do that for you.

dark trail
wraith dust
dark trail
#

yes i also do believe that adding an empty array will solve the issue. However what I'm worried about is that the code will only run once when the page first loads and analytics wont be able to capture user's events and the pages the users navigate . So instead of including an empty array dependancy , is there an alternative dependancy i can include in the array for the code to run only when the user interacts/ does certain action in the site?

#

Thankyou Bence , appreciate your output. : )