r/reactjs • • Oct 17 '26

Show /r/reactjs How do you coordinate loading states across different React components?

A local loading flag works well for one button. A reducer with context can handle a shared workflow. React Query also lets other components observe pending mutations through "useIsMutating". If one of these already covers your needs, there may be no reason to add another library.

I’m building react-action-guard to explore a reusable API for shared UI availability.

For example, imagine a document editor:

  • saving temporarily blocks editing and switching documents;
  • a local export also blocks switching documents;
  • saving and exporting can overlap;
  • finishing the save should not enable document switching while the export is still running;
  • Help stays available throughout.

Actions publish the scopes they affect:

const runSave = useAsyncAction<void>(
  "save-document",
  ["document:editing", "document:switching"]
);

const runExport = useAsyncAction<void>(
  "export-document",
  "document:switching"
);

Each operation is wrapped when it runs:

await runSave(() => saveDocument());

Independent components read the scope they care about:

const isBlocked = useIsBlocked("document:switching");

return <button disabled={isBlocked}>Switch document</button>;

Each execution has its own blocker. When operations overlap, finishing one removes only its own blocker.

You can build the same behavior with a reducer and context. The goal is to provide the registration, shared scopes, blocking reasons, and cleanup as a reusable API.

This also overlaps with React Query when availability follows mutation state. The case I’m interested in is when the same UI rules combine server operations, local tasks, and conditions such as unsaved changes.

One important detail: tracking an action does not prevent repeated execution or cancel it. Those rules still belong to the application.

I’m the author, and I’d like feedback on this approach. Have you needed this kind of coordination, or has your existing state management been enough?

GitHub

Edit: Added a more detailed example and clarified the overlap with reducers and React Query. Thanks for the feedback.

1 Upvotes

5 comments sorted by

2

u/craig1f Oct 17 '26

I haven’t done react in two years. But since most state is backend driven, the answer is react-query. 

React-query decouples your fetch from showing the result of your fetch. But it completely eliminates the need for state by caching. If you’re not using it, I recommend it.

1

u/Plenty_Background_59 Oct 17 '26

React Query is a good fit for server state, agreed. If its query and mutation states already give the UI everything it needs, I would not add another layer.

What I am exploring is coordinating which interactions should be blocked while an action is running, especially when several components are involved. Some actions may involve server requests, while others may be local.

There is definitely overlap here. For the save-profile example, React Query may already be enough. I should make the examples clearer about where this approach adds value.

1

u/craig1f Oct 17 '26

Zustand used to be my answer for any state not covered by react-query. 

You should see if that’s still the popular choice. 

1

u/[deleted] Oct 17 '26

[removed] — view removed comment

1

u/Plenty_Background_59 Oct 17 '26

That is fair. If a reducer with a status field already covers the workflow, I would not add another abstraction either.

The case I am exploring is when separate parts of the UI need to react to the same action without knowing which component started it. Named scopes give them a shared way to identify that activity.

You can build that with a reducer and context too. The value I am aiming for is a reusable API for that coordination, rather than a new state management model.

For simpler cases, I agree, it would be overkill.