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

usePromise is always called twice because of React strict mode, causing double fetches and other issues to local extensions #42

Open
remorses opened this issue Sep 12, 2024 · 0 comments

Comments

@remorses
Copy link

remorses commented Sep 12, 2024

Currently you use useEffect in usePromise to call the async function, unfortunately Raycast uses React strict mode in development and all useEffect are called twice.

Here is a simple extension to reproduce the issue:

import { useCachedPromise, usePromise } from "@raycast/utils";
import { Action, ActionPanel, List } from "@raycast/api";

export default function Command() {
  const { isLoading, data } = usePromise(async () => {
    console.log("Inside usePromise");
    return {};
  });
  const { isLoading: isLoadingCached } = useCachedPromise(async () => {
    console.log("Inside useCachedPromise");
    return { cachedData: "This is cached data" };
  });

  return (
    <List isLoading={isLoading || isLoadingCached}>
      <List.Item
        title="Result"
        subtitle={JSON.stringify(data)}
        actions={
          <ActionPanel>
            <Action.CopyToClipboard content={JSON.stringify(data)} />
          </ActionPanel>
        }
      />
    </List>
  );
}

Here is the generated logs:

15:28:49.526 Inside usePromise
15:28:49.527 Inside useCachedPromise
15:28:49.527 Inside usePromise
15:28:49.527 Inside useCachedPromise

I propose using a simple ref to track the time the promise was called. Then if the promise was already called in the last 10 milliseconds, skip running it again.

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