Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Learn React Hooks

You're reading from   Learn React Hooks Unlock scalable state, performance, and clean code with Hooks, Context, Suspense, and Form Actions

Arrow left icon
Product type Paperback
Published in May 2025
Publisher Packt
ISBN-13 9781836209171
Length 372 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
 Bugl Bugl
Author Profile Icon Bugl
Bugl
Arrow right icon
View More author details
Toc

Table of Contents (19) Chapters Close

Preface 1. Introduction to Hooks
2. Introducing React and React Hooks FREE CHAPTER 3. Using the State Hook 4. Writing Your First Application with React Hooks 5. Using Hooks With Real-World Examples
6. Using the Reducer and Effect Hooks 7. Implementing React Contexts 8. Using Hooks and React Suspense for Data Fetching 9. Using Hooks for Handling Forms 10. Using Hooks for Routing 11. Advanced Hooks Provided by React 12. Using Community Hooks 13. Refactoring and Migrating Existing Code
14. Rules of Hooks 15. Building Your Own Hooks 16. Migrating from React Class Components 17. Other Books You May Enjoy
18. Index

Creating custom API Hooks

We can also create Hooks for the various API calls. Putting these Hooks in a single file allows us to adjust the API calls easily later on. We are going to prefix our custom API Hooks with useAPI so it is easy to tell which functions are API Hooks.

Extracting custom API Hooks

Let’s create custom Hooks for our API now by following these steps:

  1. Copy the Chapter12_2 folder to a new Chapter12_3 folder by executing the following command:
    $ cp -R Chapter12_2 Chapter12_3
    
  2. Open the new Chapter12_3 folder in VS Code.
  3. Create a new src/hooks/api.js file.
  4. Edit src/hooks/api.js and import the following functions:
    import { useSuspenseQuery, useMutation } from '@tanstack/react-query'
    import {
      fetchPosts,
      fetchPost,
      searchPosts,
      createPost,
      queryClient,
    } from '@/api.js'
    
  5. Define a function to fetch posts, copied over from the code we had in src/components/post/PostFeed...
lock icon The rest of the chapter is locked

Subscribe to access the full title

Advance your knowledge in tech

A Packt subscription gives you instant online access to our library of over 8,000 practical eBooks and videos, constantly updated with the latest in tech
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Modal Close icon
Modal Close icon