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

How to Fetch API Data in React Like a Pro #1

Open
hernanytec opened this issue Jan 10, 2023 · 0 comments
Open

How to Fetch API Data in React Like a Pro #1

hernanytec opened this issue Jan 10, 2023 · 0 comments

Comments

@hernanytec
Copy link
Owner

hernanytec commented Jan 10, 2023

Building applications the user use love is not an easy task. It requires a lot of thinking about how you build your application. Using these quick but easy steps will improve your app user experience.

How This Simple Technique Will Boost User Experience?

The technique we are going to talk about is known as render as you fetch. This simple technique is also suggested by React itself while fetching data. It allows you to make your application render components concurrently while fetching data. Instead of making an entire part of an application wait for the data, you render those parts concurrently by fetching data in parallel. It will lead to a great user experience, as the user will not have to wait longer for your application to load.

Fetching Data Like a Pro

The React feature we are going to use is called Suspense. Suspense helps you with making handling async operations easy. It allows you to tell users that the expected data will be displayed. Suspense defers the execution of the entire component tree until the Promise inside the component is either resolved or rejected.

A typical example of fetching data in React:

image

Instead of using this approach, we can use a better approach by allowing React to handle async operations itself.

How to inform React that data is being fetched

To inform React that the data required by the component is being fetched, you need to throw a promise. React uses the thrown value to detect if the component is ready to be rendered. Once data is fetched, you just need to throw the data, and the component will be rendered. This very simple and quick method allows you to tell React that you want it to handle data fetching status instead of handling it yourself.

Concurrency Does The Magic

Suspense allows you to render component trees concurrently while data fetching. Suppose you have two components inside Suspense; both will start rendering in parallel. Instead of holding both component trees, the data required for both component trees will be fetched in parallel. It will lead to better performance and a great user experience.

image

Above is an example of a network request with typical data fetching in React.

image

Above is an example of a network request with data fetching using Suspense.

See the original post here.

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

1 participant