atlassian/react-sweet-state

Store is not removed from the registry in StrictMode

Closed this issue · 1 comments

hinok commented

Demo

https://codesandbox.io/p/sandbox/oncleanup-issue-not-called-m3ptjs?workspaceId=042b24d6-2010-4590-9678-d2c185d774af

Steps to reproduce

When StrictMode is disabled

  1. Disable StrictMode
  2. Click a few times Click {n} times button.
  3. Change project.

Expected
The button should display Click 0 times.

Current
The button displays Click 0 times.

When StrictMode is enabled

  1. Enable StrictMode
  2. Click a few times Click {n} times button.
  3. Change project.

Expected
The button should display Click 0 times.

Current
The button displays Click {n} times.


When Container is removed from the React tree then useEffect is triggered defined in FunctionContainer.

There is a check to see if storeState.listeners().size is equal to 0 before calling if (scope) registry.deleteStore(Store, scope);.

In StrictMode the following line

if (scope) registry.deleteStore(Store, scope);

is never called because there is always one listener and it's always that listener defined below

unsubscribe: storeState.subscribe(() => handlers.onUpdate?.()),
so it looks like unsubscribe() is not really removing listener

Affected code

Promise.resolve().then(() => {
if (
!storeState.listeners().size &&
// ensure registry has not already created a new store with same scope
storeState === registry.getStore(Store, scope, null)?.storeState
) {
handlers.onDestroy?.();
// We only delete scoped stores, as global shall persist and local are auto-deleted
if (scope) registry.deleteStore(Store, scope);
}

Fixed in v2.7.2