This seems like it's almost definitely a bug. I'm aware that the context can notify subscribers asynchronously of context changes, causing potential out-of-sync-ness. This is not that issue.
This issue is that when useContextSelector's callback function runs and returns a new value, at least sometimes, the returned value immediately afterward can still be the old value.
It's how React works, like useState too. This library is to emulate React behavior.
If you need a global variable outside React (which isn't async), various global state libraries would work for you, and Zustand would be the one.
This seems like it's almost definitely a bug. I'm aware that the context can notify subscribers asynchronously of context changes, causing potential out-of-sync-ness. This is not that issue.
This issue is that when useContextSelector's callback function runs and returns a new value, at least sometimes, the returned value immediately afterward can still be the old value.
My workaround fix is to go from this:
To this:
Note that I tested
getMemoizedContext
and its value is indeed changing when it needs to.