Skip to content
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

Incorrect behavior with React.use(promise) #6797

Open
himself65 opened this issue Jan 31, 2024 · 1 comment
Open

Incorrect behavior with React.use(promise) #6797

himself65 opened this issue Jan 31, 2024 · 1 comment

Comments

@himself65
Copy link

Describe the bug

const pollingTasks = new Map();

function Example() {
  const { data } = useSuspenseQuery({
    queryKey: ['repoData'],
    queryFn: () => {
      return axios
        .get('https://api.github.com/repos/tannerlinsley/react-query')
        .then((res) => {
          return new Promise((resolve) => {
            setTimeout(() => {
              resolve(res.data);
            }, 1000);
          });
        });
    },
  });

  const id = useId();
  const t = true;
  if (t) {
    console.log('c', !pollingTasks.has(id), t, !pollingTasks.has(id) || t);
    if (!pollingTasks.has(id) || t) {
      console.log('create new one');
      const promise = new Promise((resolve) => {
        setTimeout(() => {
          console.log('resolve');
          resolve();
        }, 1000);
      });
      pollingTasks.set(id, promise);
      use(promise);
    }
  }
  const p = pollingTasks.get(id);
  console.log('run', id);
  use(p);  // <-- should trigger infinite re-render

  return (
    <div>
      <h1>{data.name}</h1>
      <p>{data.description}</p>
      <strong>👀 {data.subscribers_count}</strong>{' '}
      <strong> {data.stargazers_count}</strong>{' '}
      <strong>🍴 {data.forks_count}</strong>
    </div>
  );
}

Your minimal, reproducible example

https://stackblitz.com/edit/tanstack-query-lb2sln?file=src%2Findex.jsx

Steps to reproduce

Just run it

Expected behavior

Should show an infinite loopIt should

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • ANY

Tanstack Query adapter

react-query

TanStack Query version

v5.18.0

TypeScript version

No response

Additional context

No response

@himself65
Copy link
Author

If you comment L28-L41, the behavior will be correct. So this is tanstack query issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant