Skip to content

Commit 98d24fe

Browse files
Reassign event listener when onPlaceSelected changes (#234)
* reassign event listener when onPlaceSelected changes * 2.7.4
1 parent 3f786b1 commit 98d24fe

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/usePlacesWidget.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,20 @@ function usePlacesWidget(props) {
9999
return function () {
100100
return event.current ? event.current.remove() : undefined;
101101
};
102-
}, []); // Autofill workaround adapted from https://stackoverflow.com/questions/29931712/chrome-autofill-covers-autocomplete-for-google-maps-api-v3/49161445#49161445
102+
}, []);
103+
(0, _react.useEffect)(function () {
104+
if (autocompleteRef.current && onPlaceSelected) {
105+
event.current = autocompleteRef.current.addListener("place_changed", function () {
106+
if (onPlaceSelected && autocompleteRef && autocompleteRef.current) {
107+
onPlaceSelected(autocompleteRef.current.getPlace(), inputRef.current, autocompleteRef.current);
108+
}
109+
});
110+
}
111+
112+
return function () {
113+
return event.current ? event.current.remove() : undefined;
114+
};
115+
}, [onPlaceSelected]); // Autofill workaround adapted from https://stackoverflow.com/questions/29931712/chrome-autofill-covers-autocomplete-for-google-maps-api-v3/49161445#49161445
103116

104117
(0, _react.useEffect)(function () {
105118
// TODO find out why react 18(strict mode) hangs the page loading

src/usePlacesWidget.js

+11
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ export default function usePlacesWidget(props) {
9494
return () => (event.current ? event.current.remove() : undefined);
9595
}, []);
9696

97+
useEffect(() => {
98+
if (autocompleteRef.current && onPlaceSelected) {
99+
event.current = autocompleteRef.current.addListener("place_changed", function () {
100+
if (onPlaceSelected && autocompleteRef && autocompleteRef.current) {
101+
onPlaceSelected(autocompleteRef.current.getPlace(), inputRef.current, autocompleteRef.current);
102+
}
103+
});
104+
}
105+
return () => (event.current ? event.current.remove() : undefined);
106+
}, [onPlaceSelected]);
107+
97108
// Autofill workaround adapted from https://stackoverflow.com/questions/29931712/chrome-autofill-covers-autocomplete-for-google-maps-api-v3/49161445#49161445
98109
useEffect(() => {
99110
// TODO find out why react 18(strict mode) hangs the page loading

0 commit comments

Comments
 (0)