-
-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error for useStore
: Warning: The result of getSnapshot should be cached to avoid an infinite loop
#22
Comments
Also getting this error using nanostore query/react with react + astro Any luck resolving it? My code for reference: https://github.com/BTS-CM/astro-ui/blob/main/src/components/common/DeepLinkDialog.jsx |
@grctest Unfortunately no—we've just shipped it with the error, which hasn't seemed to affect our site at all. But like I said above, that's more disconcerting than the error itself. Why would we be logging errors if there isn't actually an error? So far no one from nanostores has reached out to assist with this :/ |
It seems like it's a known React issue? facebook/react#24508 (comment) |
Is there any updates on this issue. I have the same with nanostores/query and nanostores/react. |
I am not using React anymore, so someone else needs to do research and find a solution. |
@ai could you fix this issue? |
@JosefJezek can use send PR? I can’t copy code from screenshots. |
my change in useStore... const getKey = () => {
const store = get();
return storeKey ? store[storeKey] : store;
};
return useSyncExternalStore(subscribe, getKey, getKey); |
@JosefJezek we don’t have |
my solution is not perfect... we need please fix |
I've encountered an same issue where export function useStoreFixed<SomeStore extends Store>(
store: SomeStore,
{ keys, deps = [store, keys] }: UseStoreOptions<SomeStore> = {},
): StoreValue<SomeStore> {
let snapshotRef = useRef() as React.MutableRefObject<StoreValue<SomeStore>>;
snapshotRef.current = store.get();
let get = () => snapshotRef.current;
return useSyncExternalStore(
useCallback(
(onChange) =>
keys?.length! > 0
? listenKeys(store as any, keys!, emit(snapshotRef, onChange))
: store.listen(emit(snapshotRef, onChange)),
deps,
),
get,
get,
);
}
const emit =
<T>(snapshot: React.MutableRefObject<T>, onChange: () => void) =>
(value: T) => {
snapshot.current = value;
onChange();
}; Ideally, this issue should be resolved on the library's end to prevent unnecessary reactivity. |
@yuyi919 can you send PR of your fix to the repo? |
The fix by @yuyi919 was released in 0.8.0. |
I'm using nanostores (
0.9.3
), in astro (1.9.0
) with react (18.0.0
).I've got this store:
Used in a React component like so:
And I get this error in the console:
I should note however that there doesn't actually appear to be any errors. The site seems to function just fine, which may actually be more disconcerting than the error itself!
My Google-Fu has failed me here as the only semi-related issue I could find was this pmndrs/zustand#1936, however it doesn't appear related as I don't have a
.getState()
function.I've also tried just using
useStore($account)
directly inside my component instead of importinguseAccount
, but the error still shows in the console.The text was updated successfully, but these errors were encountered: