ENGLISH

React Hooks in Action: With Suspense and Concurrent Mode

Book information

Publisher
Manning Publications
Year
2021
ISBN
1617297631, 9781617297632
Language
english
Format
PDF
Filesize
25 MB (26418335 bytes)
Edition
1
Pages
376\376
Time added
2021-03-20 17:11:18

Description

Build stylish, slick, and speedy-to-load user interfaces in React without writing custom classes. React Hooks are a new category of functions that help you to manage state, lifecycle, and side effects within functional components. React Hooks in Action teaches you to use pre-built hooks like useState, useReducer and useEffect to build your own hooks. Your code will be more reusable, require less boilerplate, and you’ll instantly be a more effective React developer. About the Technology Get started with React Hooks and you’ll soon have code that’s better organized and easier to maintain. React Hooks are targeted JavaScript functions that let you reuse and share functionality across components. Use them to split components into smaller functions, manage state and side effects, and access React features without classes—all without having to rearrange your component hierarchy. About the book React Hooks in Action teaches you to write fast and reusable React components using Hooks. You’ll start by learning to create component code with Hooks. Next, you’ll implement a resource booking application that demonstrates managing local state, application state, and side effects like fetching data. Code samples and illustrations make learning Hooks easy. What's inside • Build function components that access React features • Manage local, shared, and application state • Explore built-in, custom, and third-party hooks • Load, update, and cache data with React Query • Improve page and data loading with code-splitting and React Suspense About the reader For beginning to intermediate React developers. About the author John Larsen has been a teacher and web developer for over 20 years, creating apps for education and helping students learn to code. He is the author of Get Programming with JavaScript. React Hooks in Action contents preface acknowledgments about this book Who should read this book How this book is organized: A roadmap About the code liveBook discussion forum Other online resources about the author about the cover illustration Part 1 1 React is evolving 1.1 What is React? 1.1.1 Building a UI from components 1.1.2 Synchronizing state and UI 1.1.3 Understanding component types 1.2 What’s new in React? 1.3 React Hooks can add state to function components 1.3.1 Stateful function components: Less code, better organization 1.3.2 Custom hooks: Easier code reuse 1.3.3 Third-party hooks provide ready-made, well-tested functionality 1.4 Better UX with Concurrent Mode and Suspense 1.4.1 Concurrent Mode 1.4.2 Suspense 1.5 React’s new publication channels 1.6 Whom is this book for? 1.7 Getting started Summary 2 Managing component state with the useState hook 2.1 Setting up the bookings manager app 2.1.1 Generating the app skeleton with create-react-app 2.1.2 Editing the four key files 2.1.3 Adding a database file for the application 2.1.4 Creating page components and a UserPicker.js file 2.2 Storing, using, and setting values with useState 2.2.1 Assigning new values to variables doesn’t update the UI 2.2.2 Calling useState returns a value and an updater function 2.2.3 Calling the updater function replaces the previous state value 2.2.4 Passing a function to useState as the initial value 2.2.5 Using the previous state when setting the new state 2.3 Calling useState multiple times to work with multiple values 2.3.1 Using a drop-down list to set state 2.3.2 Using a check box to set state 2.4 Reviewing some function component concepts Summary 3 Managing component state with the useReducer hook 3.1 Updating multiple state values in response to a single event 3.1.1 Taking users out of the movie with unpredictable state changes 3.1.2 Keeping users in the movie with predictable state changes 3.2 Managing more complicated state with useReducer 3.2.1 Updating state using a reducer with a predefined set of actions 3.2.2 Building a reducer for the BookablesList component 3.2.3 Accessing component state and dispatching actions with useReducer 3.3 Generating the initial state with a function 3.3.1 Introducing the WeekPicker component 3.3.2 Creating utility functions to work with dates and weeks 3.3.3 Building the reducer to manage dates for the component 3.3.4 Passing an initialization function to the useReducer hook 3.3.5 Updating BookingsPage to use WeekPicker 3.4 Reviewing some useReducer concepts Summary 4 Working with side effects 4.1 Exploring the useEffect API with simple examples 4.1.1 Running side effects after every render 4.1.2 Running an effect only when a component mounts 4.1.3 Cleaning up side effects by returning a function 4.1.4 Controlling when an effect runs by specifying dependencies 4.1.5 Summarizing the ways to call the useEffect hook 4.1.6 Calling useLayoutEffect to run an effect before the browser repaints 4.2 Fetching data 4.2.1 Creating the new db.json file 4.2.2 Setting up a JSON server 4.2.3 Fetching data within a useEffect hook 4.2.4 Working with async and await 4.3 Fetching data for the BookablesList component 4.3.1 Examining the data-loading process 4.3.2 Updating the reducer to manage loading and error states 4.3.3 Creating a helper function to load data 4.3.4 Loading the bookables Summary 5 Managing component state with the useRef hook 5.1 Updating state without causing a re-render 5.1.1 Comparing useState and useRef when updating state values 5.1.2 Calling useRef 5.2 Storing timer IDs with a ref 5.3 Keeping references to DOM elements 5.3.1 Setting focus on an element in response to an event 5.3.2 Managing a text box via a ref Summary 6 Managing application state 6.1 Passing shared state to child components 6.1.1 Passing state from a parent by setting props on the children 6.1.2 Receiving state from a parent as a prop 6.1.3 Receiving an updater function from a parent as a prop 6.2 Breaking components into smaller pieces 6.2.1 Seeing components as part of a bigger app 6.2.2 Organizing multiple components within a page’s UI 6.2.3 Creating a BookableDetails component 6.3 Sharing the state and dispatch function from useReducer 6.3.1 Managing state in the BookablesView component 6.3.2 Removing an action from the reducer 6.3.3 Receiving state and dispatch in the BookablesList component 6.4 Sharing the state value and updater function from useState 6.4.1 Managing the selected bookable in the BookablesView component 6.4.2 Receiving the bookable and updater function in BookablesList 6.5 Passing functions to useCallback to avoid redefining them 6.5.1 Depending on functions we pass in as props 6.5.2 Maintaining function identity with the useCallback hook Summary 7 Managing performance with useMemo 7.1 Breaking the cook’s heart by calling, “O, shortcake!” 7.1.1 Generating anagrams with an expensive algorithm 7.1.2 Avoiding redundant function calls 7.2 Memoizing expensive function calls with useMemo 7.3 Organizing the components on the Bookings page 7.3.1 Managing the selected bookable with useState 7.3.2 Managing the selected week and booking with useReducer and useState 7.4 Efficiently building the bookings grid with useMemo 7.4.1 Generating a grid of sessions and dates 7.4.2 Generating a lookup for bookings 7.4.3 Providing a getBookings data-loading function 7.4.4 Creating the BookingsGrid component and calling useMemo 7.4.5 Coping with racing responses when fetching data in useEffect Summary 8 Managing state with the Context API 8.1 Needing state from higher up the component tree 8.1.1 Displaying a call-to-action message when the page first loads 8.1.2 Displaying booking information when a visitor selects a booking 8.1.3 Displaying an edit button for a user’s bookings: The problem 8.1.4 Displaying an edit button for a user’s bookings: The solution 8.2 Working with custom providers and multiple contexts 8.2.1 Setting an object as the context provider’s value 8.2.2 Moving the state to a custom provider 8.2.3 Working with multiple contexts 8.2.4 Specifying a default value for a context Summary 9 Creating your own hooks 9.1 Extracting functionality into custom hooks 9.1.1 Recognizing functionality that could be shared 9.1.2 Defining custom hooks outside your components 9.1.3 Calling custom hooks from custom hooks 9.2 Following the Rules of Hooks 9.2.1 Call hooks only at the top level 9.2.2 Call hooks only from React functions 9.2.3 Using an ESLint plugin for the rules of hooks 9.3 Extracting further examples of custom hooks 9.3.1 Accessing window dimensions with a useWindowSize hook 9.3.2 Getting and setting values with a useLocalStorage hook 9.4 Consuming a context value with a custom hook 9.5 Encapsulating data fetching with a custom hook 9.5.1 Creating the useFetch hook 9.5.2 Using the data, status, and error values the useFetch hook returns 9.5.3 Creating a more specialized data-fetching hook: useBookings Summary 10 Using third-party hooks 10.1 Accessing state in the URL with React Router 10.1.1 Setting up routes to enable nesting 10.1.2 Adding nested routes to the Bookables page 10.1.3 Accessing URL parameters with the useParams hook 10.1.4 Navigating with the useNavigate hook 10.2 Getting and setting query string search parameters 10.2.1 Getting search parameters from the query string 10.2.2 Setting the query string 10.3 Streamlining data-fetching with React Query 10.3.1 Introducing React Query 10.3.2 Giving components access to a React Query client 10.3.3 Fetching data with useQuery 10.3.4 Accessing data in the query cache 10.3.5 Updating server state with useMutation Summary Part 2 11 Code splitting with Suspense 11.1 Importing code dynamically with the import function 11.1.1 Setting up a web page to load JavaScript when a button is clicked 11.1.2 Using default and named exports 11.1.3 Using static imports to load JavaScript 11.1.4 Calling the import function to dynamically load JavaScript 11.2 Importing components dynamically with lazy and Suspense 11.2.1 Converting a component to a lazy component with the lazy function 11.2.2 Specifying fallback content with the Suspense component 11.2.3 Understanding how lazy and Suspense work together 11.2.4 Code splitting an app on its routes 11.3 Catching errors with error boundaries 11.3.1 Checking out the error boundary example in the React docs 11.3.2 Creating our own error boundary 11.3.3 Recovering from errors Summary 12 Integrating data fetching with Suspense 12.1 Data fetching with Suspense 12.1.1 Upgrading promises to include their status 12.1.2 Using the promise status to integrate with Suspense 12.1.3 Fetching data as early as possible 12.1.4 Fetching new data 12.1.5 Recovering from errors 12.1.6 Checking the React docs 12.2 Using Suspense and error boundaries with React Query 12.3 Loading images with Suspense 12.3.1 Using React Query and Suspense to provide an image-loading fallback 12.3.2 Prefetching images and data with React Query Summary 13 Experimenting with useTransition, useDeferredValue, and SuspenseList 13.1 Making smoother transitions between states 13.1.1 Avoiding receded states with useTransition 13.1.2 Giving users feedback with isPending 13.1.3 Integrating transitions with common components 13.1.4 Holding on to old values with useDeferredValue 13.2 Using SuspenseList to manage multiple fallbacks 13.2.1 Showing data from multiple sources 13.2.2 Controlling multiple fallbacks with SuspenseList 13.3 Concurrent Mode and the future Summary index A B C D E F G H I J K L M N O P Q R S T U V W

Similar books