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

Race condition inside a function using derived value #1

Open
Brandontam29 opened this issue Jan 19, 2023 · 1 comment
Open

Race condition inside a function using derived value #1

Brandontam29 opened this issue Jan 19, 2023 · 1 comment

Comments

@Brandontam29
Copy link

When a value in the derived store is used within a function after the original store value has been changed, there is a race condition.

Here is a simplified version my code.

const hasSoldoutItems = useDerivedBagStore((state) => state.hasSoldoutItems);
const removeSoldoutItems = useBagStore((state) => state.removeSoldoutItems);
const { handleCheckoutOpen } = useLayoutContext();

const goToCheckout = async () => {
    if (hasSoldoutItems) {
      removeSoldoutItems();
    }

    if (hasSoldoutItems) return;

    if (numberOfBagItems === 0) return;

    handleCheckoutOpen();
  };

@dai-shi
Copy link
Member

dai-shi commented Jan 20, 2023

Sounds like nice catch. Could you create a minimal reproduction with https://codesandbox.io/s/react-new ?

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

2 participants