Skip to content

Commit 74ad64c

Browse files
committed
Move initialize and boot out of effect
This reverts the changes from a6d7757
1 parent 6986ffe commit 74ad64c

1 file changed

Lines changed: 25 additions & 26 deletions

File tree

packages/react-use-intercom/src/provider.tsx

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,6 @@ export const IntercomProvider: React.FC<
4949
);
5050
}
5151

52-
const onHideWrapper = React.useCallback(() => {
53-
setIsOpen(false);
54-
if (onHide) onHide();
55-
}, [onHide, setIsOpen]);
56-
57-
const onShowWrapper = React.useCallback(() => {
58-
setIsOpen(true);
59-
if (onShow) onShow();
60-
}, [onShow, setIsOpen]);
61-
6252
const boot = React.useCallback(
6353
(props?: IntercomProps) => {
6454
if (!window.Intercom && !shouldInitialize) {
@@ -69,15 +59,26 @@ export const IntercomProvider: React.FC<
6959
return;
7060
}
7161

72-
// Attach the listeners
62+
if (isBooted.current) {
63+
return;
64+
}
65+
66+
// Register the listeners
7367
// This is done in the booth method because after shutting down
74-
// the callbacks should be re-registered
75-
IntercomAPI('onHide', onHideWrapper);
76-
IntercomAPI('onShow', onShowWrapper);
68+
// the callbacks should be re-registered on a reboot
69+
IntercomAPI('onHide', () => {
70+
setIsOpen(false);
71+
onHide?.();
72+
});
73+
IntercomAPI('onShow', () => {
74+
setIsOpen(true);
75+
onShow?.();
76+
});
7777
IntercomAPI('onUserEmailSupplied', onUserEmailSupplied);
7878

79-
if (onUnreadCountChange)
79+
if (onUnreadCountChange) {
8080
IntercomAPI('onUnreadCountChange', onUnreadCountChange);
81+
}
8182

8283
const metaData: RawIntercomBootProps = {
8384
app_id: appId,
@@ -92,25 +93,23 @@ export const IntercomProvider: React.FC<
9293
[
9394
apiBase,
9495
appId,
95-
onHideWrapper,
96-
onShowWrapper,
96+
onHide,
97+
onShow,
9798
onUnreadCountChange,
9899
onUserEmailSupplied,
99100
shouldInitialize,
100101
],
101102
);
102103

103-
React.useEffect(() => {
104-
if (!isSSR && shouldInitialize && !isInitialized.current) {
105-
initialize(appId, initializeDelay);
106-
107-
if (autoBoot) {
108-
boot(autoBootProps);
109-
}
104+
if (!isSSR && shouldInitialize && !isInitialized.current) {
105+
initialize(appId, initializeDelay);
110106

111-
isInitialized.current = true;
107+
if (autoBoot) {
108+
boot(autoBootProps);
112109
}
113-
}, [appId, autoBoot, autoBootProps, boot, initializeDelay, shouldInitialize]);
110+
111+
isInitialized.current = true;
112+
}
114113

115114
const ensureIntercom = React.useCallback(
116115
(functionName: string, callback: (() => void) | (() => string)) => {

0 commit comments

Comments
 (0)