Skip to content

Commit 2a87d0f

Browse files
committed
useRef for mounted to play nice in the react world
1 parent 94f27ea commit 2a87d0f

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

reflex/.templates/web/utils/state.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ export const useEventLoop = (
825825
const [searchParams] = useSearchParams();
826826
const [connectErrors, setConnectErrors] = useState([]);
827827
const params = useRef(paramsR);
828-
let mounted = false;
828+
const mounted = useRef(false);
829829

830830
useEffect(() => {
831831
const { "*": splat, ...remainingParams } = paramsR;
@@ -837,7 +837,7 @@ export const useEventLoop = (
837837
}, [paramsR]);
838838

839839
const ensureSocketConnected = useCallback(async () => {
840-
if (!mounted) {
840+
if (!mounted.current) {
841841
// During hot reload, some components may still have a reference to
842842
// addEvents, so avoid reconnecting the socket of an unmounted event loop.
843843
return;
@@ -859,7 +859,15 @@ export const useEventLoop = (
859859
() => params.current,
860860
);
861861
}
862-
}, [socket, dispatch, setConnectErrors, client_storage, navigate, params]);
862+
}, [
863+
socket,
864+
dispatch,
865+
setConnectErrors,
866+
client_storage,
867+
navigate,
868+
params,
869+
mounted,
870+
]);
863871

864872
// Function to add new events to the event queue.
865873
const addEvents = useCallback((events, args, event_actions) => {
@@ -962,12 +970,12 @@ export const useEventLoop = (
962970
// Handle socket connect/disconnect.
963971
useEffect(() => {
964972
// Initialize the websocket connection.
965-
mounted = true;
973+
mounted.current = true;
966974
ensureSocketConnected();
967975

968976
// Cleanup function.
969977
return () => {
970-
mounted = false;
978+
mounted.current = false;
971979
if (socket.current) {
972980
socket.current.disconnect();
973981
socket.current.off();

0 commit comments

Comments
 (0)