How To Implement The Prefetch Pattern in React Concurrent Mode

Recently, React announced a feature of the React ecosystem — Concurrent Mode. This would allow us to stop or delay the execution of components for the time that we need. It’ll help React apps stay responsive and gracefully adjust to the user’s device capabilities and network speed.

Concurrent Mode consists of a set of new features — one of the biggest ones is Suspense and a new approach to data fetching.

Basically, there are three ways to do it:

  • Fetch-on-render: We start rendering components and each of these components may trigger data fetching in their effects and lifecycle methods. A good example of that is 




  • Fetch-then-render: Start fetching all the data for the next screen as early as possible. When the data is ready, render the new screen. We can’t do anything until the data arrives. The example of that is having a 

     component that handles data fetching and conditionally renders the child presentational component once we’ve received everything.

  • Render-as-you-fetch: Start fetching all the required data for the next screen as early as possible, and start rendering the new screen immediately, even before we get a network response. As data streams in, React retries rendering components that still need data until they’re all ready.

I believe that the concepts of the first two approaches are well known and definitely presented in your code. Let’s dive straight into the render-as-you-fetch approach.

You’ve probably noticed that the explanation of this approach has two parts:

  1. Start loading data as early as possible.
  2. Start trying to render components that may still need data.

Fetch early

Let’s build an app together that loads major stock indexes. For that, we have a simple “Load” button. Once you click on it, we start loading data immediately:

const App = () => {
  const [prefetchedIndexes, setPrefetchedIndexes] = useState();

  return (
      {prefetchedIndexes && (

 is a function that performs the 


 request and returns an object that we’re going to pass to the 

 component. The key takeaway from this example is that we’re triggering fetch from the


 event and not in the render phase.

Render early with Suspense

The second part of the example above is that we’re saving the object from 


 to the state and starting to render 

 immediately as well.

On the other hand, we also don’t want to render the list with empty data, so ideally, we’d like to be able to suspend render until we have all the data without writing 

if (isLoading) return null


Suspense is a mechanism for data-fetching libraries to communicate to React that the data a component is reading is not ready yet.

React can then wait for it to be ready and update the UI.

Let me show you an example:

const IndexList = ({ prefetchedIndexes }) => {
  const data = usePrefetchedQuery(prefetchedIndexes);

  return => (
    <div key={index.ticker}>
      Show {index.ticker}

const App = () => {
  const [prefetchedIndexes, setPrefetchedIndexes] = useState();

  return (
      {prefetchedIndexes && (
        Loading indexes list...}>
To take advantage of Suspense, you just need to wrap your component with it. It accepts a 


 prop: the element that you want to show while waiting for data.

Now that you know about Suspense and prefetch practices, you wonder how this all works together. So, here is the last piece of this puzzle. To solve it, let’s finally check out the 



function wrapPromise(promise) {
  let status = "pending";
  let result;
  let suspender = promise.then(
    r => {
      status = "success";
      result = r;
    e => {
      status = "error";
      result = e;
  return {
    read() {
      if (status === "pending") {
        throw suspender;
      } else if (status === "error") {
        throw result;
      } else if (status === "success") {
        return result;

// Function that reads resource object
// It could also include Cache logic
export const usePrefetchedQuery = prefetchedQuery =>;

export const prefetchQuery = (input, init) => {
  // Make fetch request
  const promise = fetch(input, init).then(response => response.json());

  // Return resource for Suspense
  return wrapPromise(promise);

Don’t be scared by the complexity of it, it’s actually fairly simple.

First, we take a URL and pass it to the native 


 function, receive a promise, and pass it to the 



This function returns an object with the 



  • If a promise is still pending, we throw this promise.
  • If a promise is resolved with error, we throw the error.
  • If a promise is resolved, just return the data.

In fact, the only difference we have, compared to traditional fetching practices, is throwing a pending promise.

When you have 




, it just executes the 


 method. If data is not there yet, it throws a promise before actually rendering anything and 


 will catch that.

For that, you need to run 

npm i [email protected] [email protected]

 and play with it locally. I also created a live example on CodeSandbox for you that shows everything I did together in one working project.

No. Concurrent mode is still under development and some implementation details could change. Use experimental versions to get familiar with new concepts and maybe propose your own ideas.

For example, how to integrate prefetch practices in routers or provide a good way to cache data.

read original article here