Closed harrynikolov closed 1 year ago
Hi @harrynikolov, the problem might reside in your code, RoundRequestManager
component:
useEffect(() => {
if (currentRound && !hasPermission) {
requestPermissionForRound();
// We can say with certainty that currentRound is NOT undefined when this is called
}
}, [hasPermission, currentRound]);
It uses requestPermissionForRound
but not declares it as a dependency. This code still works if currentRound
changes at the same render cycle together with requestPermissionForRound
, but it is not the case for react-singleton-hook
. The library introduces a layer of indirection to allow many subscribers and will trigger another render with a new value when it propagates.
I hope it solves the problem. If it is not, and you still believe there is a caching issue incidence the library, I'd be happy to debug an MRE.
@Light-Keeper I have encountered this issue as well. Please see https://stackblitz.com/edit/react-ts-merrh3?file=hooks/useTest.ts.
This is representative of my use-case, and as you can see the value that is included as a dependency of the useCallback is not updated, even though the useEffect in that same file gets the updated value.
And just in case the issue was with my implementation of ReduxStore, here is another example in which I use react-redux library directly, and wrap <SingletonHooksContainer/>
in the react-redux Provider
and the behavior is the same.
Please advise 🙏
Yikes, nevermind. The listener created a closure that no longer had access to the state being updated. I feel like I just failed a coding interview 🤦♀️ Please disregard.
Hello! We have a strange bug, where functions created inside a singleton-hook appear to be outdated. We have proved that the singleton-hook recognizes a Redux state update AND re-renders. But then, when one of the singleton-hook's functions is called by a consumer, it does not appear to be calling the most up-to-date version of the function.
I'm wondering if you can help? Do singleton-hooks memoize their outputs behind the scenes; or what else could be causing the singleton-hook to return a stale function?
We have confirmed that making this hook to NOT be a singleton-hook solves the problem. (see console.logs below)
Attached below is some example code (contains some pseudocode for simplicity)