Knowledgebase

Managing Server Data Separately Print

  • frontenddevelopment, frontend, caching, performance, errors, guide, howto, solution
  • 0

Data from an API is not application state.

WHY IT IS DIFFERENT

It is owned elsewhere It can become stale It needs caching, refetching and invalidation It has loading and error states

WHAT HAPPENS WHEN IT IS TREATED AS ORDINARY STATE

Manual loading flags everywhere Duplicate requests Stale data with no refresh strategy Substantial repeated code

WHAT DATA-FETCHING LIBRARIES PROVIDE

Caching by key Deduplication of identical concurrent requests Background refetching Loading and error states Invalidation after mutations Retry behaviour

WHY THAT MATTERS

It removes the majority of the code people write around fetching.

WHAT TO DEFINE

A key for each piece of data How long it is considered fresh What invalidates it after a change

WHAT TO DO AFTER A MUTATION

Invalidate what it affected, so it refetches.

WHAT OPTIMISTIC UPDATES ARE

Updating the interface before the server confirms.

WHAT THEY REQUIRE

A way to revert on failure.

WHAT TO ALWAYS HANDLE

Failure, and slow responses.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot