@geophrase/react 1.0.5 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/useGeophrase.js +13 -13
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@geophrase/react",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "React and Next.js hooks for the Geophrase Connect SDK",
|
|
5
5
|
"homepage": "https://business.geophrase.com/docs",
|
|
6
6
|
"repository": {
|
|
@@ -32,6 +32,6 @@
|
|
|
32
32
|
"react": ">=16.8.0"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@geophrase/core": "
|
|
35
|
+
"@geophrase/core": "^1.1.0"
|
|
36
36
|
}
|
|
37
37
|
}
|
package/src/useGeophrase.js
CHANGED
|
@@ -6,16 +6,12 @@ import Geophrase from '@geophrase/core';
|
|
|
6
6
|
export const useGeophrase = (options = {}) => {
|
|
7
7
|
const geoInstance = useRef(null);
|
|
8
8
|
|
|
9
|
-
// 1. Keep a mutable reference to the latest callbacks
|
|
10
|
-
// This prevents React from destroying/recreating the iframe just
|
|
11
|
-
// because the parent component re-rendered and created a new function.
|
|
12
9
|
const savedCallbacks = useRef({
|
|
13
10
|
onSuccess: options.onSuccess,
|
|
14
11
|
onError: options.onError,
|
|
15
12
|
onClose: options.onClose
|
|
16
13
|
});
|
|
17
14
|
|
|
18
|
-
// 2. Update the callbacks on every render without triggering the useEffect
|
|
19
15
|
useEffect(() => {
|
|
20
16
|
savedCallbacks.current = {
|
|
21
17
|
onSuccess: options.onSuccess,
|
|
@@ -24,16 +20,18 @@ export const useGeophrase = (options = {}) => {
|
|
|
24
20
|
};
|
|
25
21
|
});
|
|
26
22
|
|
|
27
|
-
// 3. Manage the Vanilla JS Lifecycle safely
|
|
28
23
|
useEffect(() => {
|
|
29
24
|
if (typeof window === 'undefined') return;
|
|
30
25
|
|
|
31
|
-
//
|
|
26
|
+
// 1. Strict Developer Warning
|
|
27
|
+
if (!options.key) {
|
|
28
|
+
console.warn("Geophrase: 'key' is required to initialize the widget.");
|
|
29
|
+
}
|
|
30
|
+
|
|
32
31
|
geoInstance.current = new Geophrase({
|
|
33
32
|
key: options.key,
|
|
34
33
|
order_id: options.order_id,
|
|
35
34
|
phone: options.phone,
|
|
36
|
-
// Pass wrapper functions to always execute the latest React state
|
|
37
35
|
onSuccess: (data) => {
|
|
38
36
|
if (savedCallbacks.current.onSuccess) savedCallbacks.current.onSuccess(data);
|
|
39
37
|
},
|
|
@@ -45,19 +43,14 @@ export const useGeophrase = (options = {}) => {
|
|
|
45
43
|
}
|
|
46
44
|
});
|
|
47
45
|
|
|
48
|
-
// Cleanup: Destroy the DOM elements if the React component unmounts
|
|
49
|
-
// This perfectly handles React 18's Strict Mode double-mount behavior
|
|
50
46
|
return () => {
|
|
51
47
|
if (geoInstance.current) {
|
|
52
48
|
geoInstance.current.destroy();
|
|
53
49
|
geoInstance.current = null;
|
|
54
50
|
}
|
|
55
51
|
};
|
|
56
|
-
|
|
57
|
-
// We only re-run this effect if the CORE configuration changes
|
|
58
52
|
}, [options.key, options.order_id, options.phone]);
|
|
59
53
|
|
|
60
|
-
// 4. Provide a stable reference to the open method
|
|
61
54
|
const open = useCallback(() => {
|
|
62
55
|
if (geoInstance.current) {
|
|
63
56
|
geoInstance.current.open();
|
|
@@ -66,5 +59,12 @@ export const useGeophrase = (options = {}) => {
|
|
|
66
59
|
}
|
|
67
60
|
}, []);
|
|
68
61
|
|
|
69
|
-
|
|
62
|
+
// 2. Fixed missing close implementation
|
|
63
|
+
const close = useCallback(() => {
|
|
64
|
+
if (geoInstance.current) {
|
|
65
|
+
geoInstance.current.close();
|
|
66
|
+
}
|
|
67
|
+
}, []);
|
|
68
|
+
|
|
69
|
+
return { open, close };
|
|
70
70
|
};
|