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
Learn React Hooks
Learn React Hooks

Learn React Hooks: Unlock scalable state, performance, and clean code with Hooks, Context, Suspense, and Form Actions , Second Edition

Arrow left icon
Profile Icon Daniel Bugl
Arrow right icon
$29.99 $33.99
eBook May 2025 372 pages 2nd Edition
eBook
$29.99 $33.99
Paperback
$41.99
Subscription
Free Trial
Renews at $19.99p/m
Arrow left icon
Profile Icon Daniel Bugl
Arrow right icon
$29.99 $33.99
eBook May 2025 372 pages 2nd Edition
eBook
$29.99 $33.99
Paperback
$41.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
$29.99 $33.99
Paperback
$41.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Learn React Hooks

Introducing React and React Hooks

React is a JavaScript library used to build efficient and extensible web applications. React was developed by Meta and is used in many large-scale web applications, such as Facebook, Instagram, Netflix, Shopify, Airbnb, Cloudflare, and the BBC.

In this book, we are going to learn how to build complex and efficient user interfaces with React, while keeping the code simple and extensible. Using the paradigm of React Hooks, we can greatly simplify dealing with state and effects in web applications, ensuring the potential for growing and extending the application later. We are also going to learn about React Context, React Suspense, and Form Actions, as well as how they can be used with Hooks. Finally, we are going to learn how to build our own Hooks and how to migrate existing applications from React class components to a React Hooks-based architecture.

In this first chapter, we are going to learn about the fundamental principles of React and...

Technical requirements

A fairly recent version of Node.js should already be installed. The Node Package Manager (npm) also needs to be installed (it should come with Node.js). For more information on how to install Node.js, please check out their official website: https://nodejs.org/.

We are going to use Visual Studio Code (VS Code) for the guides in this book, but everything should work similarly in any other editor. For more information on how to install VS Code, please refer to their official website: https://code.visualstudio.com.

In this book, we use the following versions:

  • Node.js v22.14.0
  • npm v10.9.2
  • Visual Studio Code v1.97.2

The versions mentioned in the preceding list are the ones used in the book. While installing a newer version should not be an issue, please note that certain steps might work differently on a newer version. If you are having an issue with the code and steps provided in this book, please try using the mentioned versions...

Principles of React

Before we start learning how to set up a React project, let’s revisit the three fundamental principles of React. These principles allow us to easily write scalable web applications.

React is based on three fundamental principles:

  • Declarative: Instead of telling React how to do things, we tell it what we want it to do. For example, if we change data, we don’t need to tell React which components need to be re-rendered. That would be complex and error-prone. Instead, we just tell React that data has changed and all components using this data will be efficiently updated and re-rendered for us. React takes care of the details so that we can focus on the tasks at hand to easily develop our web application.
  • Component-based: React encapsulates components that manage their own state and views and then allows us to compose them in order to create complex user interfaces.
  • Learn once, write anywhere: React does not make assumptions...

Motivation to use React Hooks

React always strives to make the developer experience as smooth as possible while ensuring to keep it performant enough, without the developer having to worry too much about how to optimize performance. However, throughout the years of using React, a couple of problems have been identified.

The code snippets in the following subsections are just intended to give you an understanding of why Hooks were needed, by giving examples of how developers previously dealt with certain problems in React. If you are not familiar with those old ways, do not worry, it’s not necessary to understand the old ways to follow along. In the upcoming sections, we are going to learn how to deal with these problems in a better way using React Hooks.

Let’s now take a look at these problems in the following subsections.

Confusing classes

In the past, we had to use class components with special functions called life cycle methods, such as...

Setting up the development environment

In this book, we are going to use VS Code as our code editor. Feel free to use whichever editor you prefer, but keep in mind that the extensions used and settings configured may be slightly different in the editor of your choice.

Let’s now install VS Code and some useful extensions, and then continue setting up all the tools needed for our development environment.

Installing VS Code and extensions

Before we can get started developing and setting up the other tools, we need to set up our code editor, by following these steps:

  1. Download VS Code for your operating system from the official website (at the time of writing, the URL is https://code.visualstudio.com/). We are going to use version 1.97.2 in this book.
  2. After downloading and installing the application, open it, and you should see the following window:
Figure 1.1 – A fresh installation of Visual Studio Code (on macOS)

Figure 1.1 – A fresh installation of Visual Studio Code (on macOS)

  1. To...

Getting started with React Hooks

As we learned earlier in this chapter, React Hooks solve many problems, especially in larger web applications. Hooks were added in React 16.8, and they allow us to use state, and various other React features, without writing a class. In this section, we will start by defining a class component, and then we will write the same component as a function component using Hooks. We will then discuss the advantages of Hooks and how to migrate from classes to a Hook-based solution.

Starting with a class component

Let’s start by creating a traditional React class component, which lets us enter a name; this is then displayed in our app:

  1. Copy the Chapter01_1 folder to a new Chapter01_2 folder, as follows:
    $ cp -R Chapter01_1 Chapter01_2
    

    On macOS, it is important to run the command with a capitalized -R flag, not -r. The -r flag deals differently with symlinks and causes the node_modules/ folder to break. The...

Summary

In this first chapter of the book, we started by learning about the fundamental principles of React, and which types of components it provides. We then moved on to learn about common problems with class components, using existing features of React, and how they break the fundamental principles. Next, we implemented a simple application using class components and function components with Hooks, in order to be able to compare the differences between the two solutions. As we found out, function components with Hooks are a much better fit for React’s fundamental principles; they do not suffer from the same problems as class components, and they make our code much more concise and easy to understand! The React team now even recommends using function components instead of class components, making function components the state-of-the-art way to write React code. After reading this chapter, the basics of React and React Hooks are clear. We can now move on to learn about Hooks...

Questions

To recap what we have learned in this chapter, try to answer the following questions:

  1. What are React’s three fundamental principles?
  2. What are the two types of components in React?
  3. What are the problems with class components in React?
  4. What is the problem when using higher-order components in React?
  5. Which tool can we use to set up a React project, and what is the command that we need to run to use it?
  6. What do we need to do if we get the following error with class components: Uncaught TypeError: Cannot read properties of undefined (reading 'setState')?
  7. How do we use React state with Hooks?
  8. What are the advantages of using function components with Hooks, in comparison to class components?
  9. Do we need to replace all class components with function components using Hooks when updating React?

Further reading

If you are interested in more information about the concepts that we have learned in this chapter, take a look at the following links:

Learn more on Discord

To join the Discord community for this book – where you can share feedback, ask questions to the author, and learn about new releases – follow the QR code below:

https://packt.link/wnXT0

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Build custom Hooks to simplify complex logic and promote code reusability
  • Transform class components into modern, hook-based function components, and write robust tests
  • Build maintainable, production-ready UIs using React 19's declarative approach and ecosystem tools
  • Purchase of the print or Kindle book includes a free PDF eBook

Description

React Hooks allow you to easily encapsulate, reuse, and refactor logic with the same level of simplicity that components bring to user interface organization. In this second edition, React expert and author of many popular React books, Daniel Bugl guides you through the full spectrum of React Hooks, and teaches you how to handle forms, routing, and data fetching in modern React development. This edition is fully updated to React 19, with a revamped structure, expanded real-world use cases, and coverage of all newly introduced Hooks. Starting with the fundamentals, you'll gain a deep understanding of how Hooks work under the hood and how to apply Hooks patterns efficiently. The chapters guide you through using State, Reducer, and Effect Hooks to manage application state, side effects, and complex logic for building your first Hook-based app. You'll utilize Suspense and Context APIs for streamlined data fetching and state management, master Form Actions for handling submissions, and implement routing with Hooks. You’ll also create custom Hooks for advanced functionality and write tests for reliability. Finally, you’ll learn how to refactor an existing app with class components into modern Hooks architecture. By the end of this React book, you'll be well-equipped to use React Hooks to modernize and optimize your React projects.

Who is this book for?

This book is ideal for React developers looking to modernize their applications using React Hooks, Context, and Suspense. Beginners and experienced developers alike will gain a solid understanding of Hooks and their internal workings. If you're familiar with React and JavaScript, this book will help you upskill by teaching you best practices, performance optimization, and scalable application building. No prior experience with Hooks is required—this book covers everything from fundamentals to advanced patterns.

What you will learn

  • Master and apply new React 19 Hooks, such as useActionState, useFormStatus, useOptimistic, and others
  • Use React Hooks and Context to manage state in your web applications
  • Efficiently fetch, update, and cache data using TanStack Query and Suspense
  • Manage user input and form submission using Form Actions with Hooks
  • Discover how to implement routing in your apps using React Router and Hooks
  • Create and test your own Hooks to encapsulate, reuse, and refactor logic in your apps

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : May 23, 2025
Length: 372 pages
Edition : 2nd
Language : English
ISBN-13 : 9781836209164
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : May 23, 2025
Length: 372 pages
Edition : 2nd
Language : English
ISBN-13 : 9781836209164
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Table of Contents

18 Chapters
Introduction to Hooks Chevron down icon Chevron up icon
Introducing React and React Hooks Chevron down icon Chevron up icon
Using the State Hook Chevron down icon Chevron up icon
Writing Your First Application with React Hooks Chevron down icon Chevron up icon
Using Hooks With Real-World Examples Chevron down icon Chevron up icon
Using the Reducer and Effect Hooks Chevron down icon Chevron up icon
Implementing React Contexts Chevron down icon Chevron up icon
Using Hooks and React Suspense for Data Fetching Chevron down icon Chevron up icon
Using Hooks for Handling Forms Chevron down icon Chevron up icon
Using Hooks for Routing Chevron down icon Chevron up icon
Advanced Hooks Provided by React Chevron down icon Chevron up icon
Using Community Hooks Chevron down icon Chevron up icon
Refactoring and Migrating Existing Code Chevron down icon Chevron up icon
Rules of Hooks Chevron down icon Chevron up icon
Building Your Own Hooks Chevron down icon Chevron up icon
Migrating from React Class Components Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.