Book Image

Designing React Hooks the Right Way

By : Fang Jin
Book Image

Designing React Hooks the Right Way

By: Fang Jin

Overview of this book

React hook creates a unique solution for using states in function components to orchestrate UI communication. They provide you with an easy interface to write custom data management solutions with low development and maintenance costs. Understanding how Hooks are designed enables you to use them more effectively, and this book helps you to do just that. This book starts with a custom-crafted solution to reveal why Hooks are needed in the first place. You will learn about the React engine and discover how each built-in Hook can manage a persistent value by hooking into it. You will walk through the design and implementation of each hook with code so that you gain a solid understanding. Finally, you'll get to grips with each Hook's pitfalls and find out how to effectively overcome them. By the end of this React book, you'll have gained the confidence to build and write Hooks for developing functional and efficient web applications at scale.
Table of Contents (12 chapters)

Reviewing React hooks

We have seen quite a few hooks provided by React. Let's take a moment to review what we have learned so far:

  • Update a state with the useState hook.
  • Handle a side effect with the useEffect hook.
  • Reuse the last value with the useMemo hook.
  • Update an area with the useContext hook.
  • Hide stuff from display with the useRef hook.

The useState hook is the most popular one, which is used to define a state and make it dispatchable to trigger a UI update. React wants us to use this as the main mechanism to be in sync with the screen. A mental picture of using it is that, as long as the state changes, the UI should produce an outcome accordingly. Otherwise, the UI should stay intact. Essentially, what that is saying is to make something happen on the screen, design a state and wire it with elements. This is the React way. If you take this as a baseline, it can help you understand anything else.

The useEffect hook allows us to listen...