Skip to content

Commit

Permalink
Fix useEffect loop
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbarbara committed Jul 12, 2021
1 parent 5e263cc commit 2b6f5da
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReactNode, useCallback, useEffect } from 'react';
import { ReactNode, useEffect, useRef } from 'react';
import { createBrowserHistory, History } from 'history';
import WebSession, { AnyObject, Options, Session } from 'web-session';

Expand All @@ -23,9 +23,9 @@ function ReactWebSession(props: Props) {
timezone = 'UTC',
} = props;

const setData = useCallback(() => {
const setData = useRef(() => {
webSession.update(data);
}, [data]);
});

useEffect(() => {
webSession.init({
Expand All @@ -34,16 +34,16 @@ function ReactWebSession(props: Props) {
name,
timezone,
});
setData();
setData.current();

const removeListener = history.listen(() => {
setData();
setData.current();
});

return () => {
removeListener();
};
}, [callback, duration, history, name, setData, timezone]);
}, [callback, duration, history, name, timezone]);

if (typeof children === 'function') {
return children(webSession.session, history);
Expand All @@ -53,3 +53,4 @@ function ReactWebSession(props: Props) {
}

export default ReactWebSession;
export { Session };

0 comments on commit 2b6f5da

Please sign in to comment.