DEV Community

Daksh Kulshrestha
Daksh Kulshrestha

Posted on

2

Suspense in react

So, What is Suspense in React?

A very simple way to put it is that it is used as a event listener for data fetching.

Umm... what? what did you just say?

Let me explain,

When we would use a third party API to make calls, it takes a bit time to get the response, to tell the user that it's loading, what one would do is make a state and set it to a boolean and change it when data is fetched and use that state to display a spinner/loader.
This seems pretty easy to do, but what happens when you're in a very big application?
Making a state, updating it's value, showing spinner would be tedious. That's where Suspense comes into play.

Suspense API knows when your data arrives, you just have to setup some boilerplate code once and you're set!

Suspense knows when your data is still loading and it shows a fallback component till the time being, like a spinner or preloader for example.

BEWARE! This is available in an experimental version of React, do not implement this is your production level apps.

For those who'd like to play around with this, here is a link to a codesandbox instance, fork it and experiment!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay