Skip to main content

Command Palette

Search for a command to run...

A Look At React Hooks: The New `use` Hook

A new and life-changing Hook coming soon for React!

Updated
A Look At React Hooks: The New `use` Hook
V

I'm a solutions engineer lead, GitHub Star, Director of WomenDevsSG, and co founder of ragTech. I work at the intersection of tech, systems, and leadership, and this blog is where I share my journey through all three. Expect honest reflections, real experiences, and thoughts that are still forming rather than polished career advice.

Welcome to another article of A Look at React Hooks, a beginner-friendly series on React Hooks. In this article, let's learn about the use Hook. That's right, that's the Hook name.

Note that this Hook is not yet available officially in React. If you would like to test it out, make sure your react and react-dom package version is set to "experimental".

Without further ado, let's talk about this new Hook!

Edit: This hook is now in the latest React 19!

About use Hook

The use Hook was revealed back in 2022 by the React team in an RFC (Request For Comments). It was a solution designed to simplify the process of data fetching by reducing the amount of boilerplate code needed to define Promises.

As you know, data fetching in React requires us to handle different states such as loading, error handling, etc. This makes the code quite chunky. Here's a typical example using useSWR from my A Look At React Hooks: useSWR for Data Fetching article.

import useSWR from "swr";
function App() {
  const fetcher = (...args) => fetch(...args).then((res) => res.json());

  // fetch data
  const { data, error } = useSWR(
    "https://jsonplaceholder.typicode.com/todos/1",
    fetcher
  );

  if (error) return <div>failed to load</div>;
  if (!data) return <div>loading...</div>;

  // render data
  return (
    <div className="App">
      <h2>{data.title}</h2>
      <p>UserId: {data.userId}</p>
      <p>Id: {data.id}</p>
      {data.completed? <p>Completed</p> : <p>Not Completed</p>}
    </div>
  );
}

export default App;

How to use use Hook

According to the RFC, the use Hook can be used like await .

// `use` inside of a React component or Hook...
const data = use(promise);

// ...roughly equates to `await` in an async function
const data = await promise;

Unlike other Hooks, use Hook has a special trait where it can be called inside conditions, blocks, and loops.

This makes it a flexible Hook when you want to add logic and conditional flow without adding more components.

Example Usage

Below, we see an example provided by the RFC on how this Hook can be used. With this Hook, a promise can be passed as an argument, enabling data to be fetched asynchronously. Additionally, the "use" Hook can be used inside an if statement, making it possible to conditionally suspend data based on certain conditions.

import { use } from "react";

function Note({id, shouldIncludeAuthor}) {
  // fetching data asynchronously
  const note = use(fetchNote(id));

  let byline = null;
  if (shouldIncludeAuthor) {
    // can be used inside if statements
    // Because `use` is inside a conditional block, we avoid blocking
    // unncessarily when `shouldIncludeAuthor` is false.
    const author = use(fetchNoteAuthor(note.authorId));
    byline = <h2>{author.displayName}</h2>;
  }

  return (
    <div>
      <h1>{note.title}</h1>
      {byline}
      <section>{note.body}</section>
    </div>
  );
}

Conclusion

In this article, we discussed some of the features and uses of the experimental use Hook. It can be a great alternative for data fetching in React.

Thank you for reading, I hope it has been a useful introduction to use and if you are curious, be sure to check out the original RFC documentation below. Cheers!

Let's Connect!

P
Praveen V2y ago

https://devtohash.hashnode.dev/javascript-vs-typescript-comparison-analysis

1
P
Praveen V2y ago

JS vs TS with code

1
P
Praveen V2y ago

https://devtohash.hashnode.dev/javascript-vs-typescript-comparison-analysis

1
P
Praveen V2y ago

JS vs TS Overview

1
K

hi, please can you help me out with a project?

P
Praveen V2y ago

https://devtohash.hashnode.dev/javascript-vs-typescript-comparison-analysis

K
KOTOBA AI2y ago

I tried to use the use hook in TSX but even the latest @types/react doesn't have a type definition for that hook and it leads to extremely poor SEO due to the fact that the hook doesn't go to the latest version, I can't find any information about this issue. Have you encountered this problem please?

V

Hi KOTOBA AI, this Hook is not yet available officially in React. If you would like to test it out, make sure your react and react-dom package version is set to "experimental".

K
KOTOBA AI2y ago

Hi Victoria Lo, I have found the solution by adding the following field in tsconfig.json:

"compilerOptions": {
    "types": ["react/canary"],
  }
1
V

Awesome KOTOBA AI ! thanks for letting me know! :)

W

This is an excellent post I seen thanks to share it. It is really what I wanted to see hope in future you will continue for sharing such a dinosaur game excellent post.

G

Well explained Victorial ✨ Short & straightforward.

1
V

Thank you Ganesh 😊

A Look at React Hooks

Part 2 of 16

In this series, we shall look at various React Hooks and learn how we can use them to implement in our React projects! Great for beginners in React or React Hooks.

Up next

A Look at React Hooks: useNavigation

Handle page navigation and behaviours with useNavigation!