Skip to content

Using async derived atom from atomWithSuspenseQuery calls queryFn infinitely, resulting in keeping triggering Suspense. #149

@hyp3rflow

Description

@hyp3rflow

codesandbox: https://codesandbox.io/p/sandbox/jotai-tanstack-query-atomwithsuspensequery-infinite-fetching-4dmrp5

It seems that the bug happens on React 19+, downgrading to 18 relieves issue.
I think #96 and #99 are maybe similar issue, but the difference is we are not using any try/catch logic or getting error from query here.

import { atom, useAtomValue } from "jotai";
import { atomWithSuspenseQuery } from "jotai-tanstack-query";
import { Suspense } from "react";

const App = () => {
  return (
    <Suspense fallback={<p>fallback</p>}>
      <Comp />
    </Suspense>
  );
};

const baseAtom = atomWithSuspenseQuery(() => {
  return {
    queryKey: ["base"],
    queryFn: async () => {
      let r: ((v: unknown) => void) | undefined = undefined;
      const p = new Promise((res) => (r = res));
      setTimeout(() => r?.(""), 1000);
      return p;
    },
  };
});
const derivedAtom = atom(async (get) => {
  return await get(baseAtom);
});
const Comp = () => {
  useAtomValue(derivedAtom);
  return <p>merong</p>;
};

export default App;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions