Skip to content

Commit

Permalink
fix skipFirstRun at useCustomEvent in strict mode (#55)
Browse files Browse the repository at this point in the history
Co-authored-by: Sándor Szilágyi <[email protected]>
  • Loading branch information
szilagyi-sandor and Sándor Szilágyi authored Sep 26, 2023
1 parent 22b3e33 commit 425fd53
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions applicationinsights-react-js/src/useTrackEvent.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* ReactPlugin.ts
* @copyright Microsoft 2019
*/
* ReactPlugin.ts
* @copyright Microsoft 2019
*/
import ReactPlugin from "./ReactPlugin";
import { Dispatch, SetStateAction, useEffect, useRef, useState } from "react";

Expand All @@ -15,6 +15,7 @@ export default function useCustomEvent<T>(
): AIReactCustomEvent<T> {
const [data, setData] = useState(eventData);
const firstRun = useRef(skipFirstRun);
const savedSkipFirstRun = useRef(skipFirstRun);

useEffect(() => {
if (firstRun.current) {
Expand All @@ -24,5 +25,11 @@ export default function useCustomEvent<T>(
reactPlugin.trackEvent({ name: eventName }, data);
}, [reactPlugin, data, eventName]);

useEffect(() => {
return () => {
firstRun.current = savedSkipFirstRun.current;
};
}, []);

return setData as AIReactCustomEvent<T>;
}

0 comments on commit 425fd53

Please sign in to comment.