Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

2.6 KB
Raw Blame
diff --git a/lib/index.cjs.js b/lib/index.cjs.js
index 917e1c5..0c70918 100644
--- a/lib/index.cjs.js
+++ b/lib/index.cjs.js
@@ -612,9 +612,27 @@ var isPaused = function isPaused() {
 var getStoreState$1 = function getStoreState() {
   return getContext().store.getState();
 };
+var lastSelectableStore;
+var lastSelectableStoreState;
 function useSelector(selector) {
   return shim$1.exports.useSyncExternalStore(getContext().store.subscribe, function () {
-    return selector(getStoreState$1());
+    // PostHog patch: redux getState() throws while a dispatch is running, and React can pull
+    // a render snapshot in that window (seen on Firefox). The pre-dispatch state is the correct
+    // snapshot, and the store notifies again once the dispatch commits. The fallback is scoped
+    // to the store instance so a context reset can never serve state from a previous store.
+    var store = getContext().store;
+    var storeState;
+    try {
+      storeState = store.getState();
+      lastSelectableStore = store;
+      lastSelectableStoreState = storeState;
+    } catch (error) {
+      if (lastSelectableStore !== store || lastSelectableStoreState === undefined) {
+        throw error;
+      }
+      storeState = lastSelectableStoreState;
+    }
+    return selector(storeState);
   });
 }
 function useValues(logic) {
diff --git a/lib/index.esm.js b/lib/index.esm.js
index 8116790..3500e6a 100644
--- a/lib/index.esm.js
+++ b/lib/index.esm.js
@@ -586,9 +586,27 @@ var isPaused = function isPaused() {
 var getStoreState$1 = function getStoreState() {
   return getContext().store.getState();
 };
+var lastSelectableStore;
+var lastSelectableStoreState;
 function useSelector(selector) {
   return shim$1.exports.useSyncExternalStore(getContext().store.subscribe, function () {
-    return selector(getStoreState$1());
+    // PostHog patch: redux getState() throws while a dispatch is running, and React can pull
+    // a render snapshot in that window (seen on Firefox). The pre-dispatch state is the correct
+    // snapshot, and the store notifies again once the dispatch commits. The fallback is scoped
+    // to the store instance so a context reset can never serve state from a previous store.
+    var store = getContext().store;
+    var storeState;
+    try {
+      storeState = store.getState();
+      lastSelectableStore = store;
+      lastSelectableStoreState = storeState;
+    } catch (error) {
+      if (lastSelectableStore !== store || lastSelectableStoreState === undefined) {
+        throw error;
+      }
+      storeState = lastSelectableStoreState;
+    }
+    return selector(storeState);
   });
 }
 function useValues(logic) {