Click here to Skip to main content
15,748,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to call this logEvent function inside useEffect hook only when the searchTerms string is changed. The problem is that total_result is equal to the previous state of value. So I need somehow get the actual latest value of data? Is it possible to do it without making useEffect like this:

What I have tried:

useEffect(() => {
    if (searchTerms) {
      analyticsService?.logEvent(ANALYTICS_EVENTS.SEARCH_COMPLETED, {
        total_results: data?,
        error_message: null,
  }, [searchTerms, data?]);

Because in some cases the total_results can be the same, so it will not call my logEvent function.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900