Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Designing React Hooks the Right Way

You're reading from   Designing React Hooks the Right Way Explore design techniques and solutions to debunk the myths about adopting states using React Hooks

Arrow left icon
Product type Paperback
Published in Jan 2022
Publisher Packt
ISBN-13 9781803235950
Length 278 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Fang Jin Fang Jin
Author Profile Icon Fang Jin
Fang Jin
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Chapter 1: Introducing the Function Component 2. Chapter 2: Crafting States in Functions FREE CHAPTER 3. Chapter 3: Hooking into React 4. Chapter 4: Use State to Jumpstart Components 5. Chapter 5: Use Effect to Handle Side Effects 6. Chapter 6: Use Memo to Boost Performance 7. Chapter 7: Use Context to Cover an Area 8. Chapter 8: Use Ref to Hide Stuff 9. Chapter 9: Use Custom Hooks to Reuse Logic 10. Chapter 10: Building a Website with React 11. Other Books You May Enjoy

How states work with UI

With the introduction of state to the function component, we sometimes can get dizzy by the roles that it plays. We will use three components to elaborate, as shown in Figure 2.11:

Figure 2.11 – Props in components

Figure 2.11 – Props in components

We have three components depicted in solid boxes. The outer component contains the middle one as a child, and the middle one contains the inner one as a child. Props, depicted as arrow lines crossing the boundary of a solid box, pass values from a parent to a child component.

React is a state machine. For a given fixed set of variables, it paints the screen the same way. With props, this is quite straightforward since each component is solely determined by its props. Now, let's add the states to the picture, as shown in Figure 2.12. States, depicted as a symbol with a circle and a dot, are defined inside each component:

Figure 2.12 – States and props in components

Figure 2.12 – States and props in components

Taking the C inner component first, it doesn't have any state defined. So, it's still determined by its props.

The B middle component has one state defined. With a fixed set of its props, the screen corresponding to the component still can vary because this state can take a different value on each update.

The A outer component has two states defined. Similarly, with all its props fixed, the screen corresponding to it can still vary. The variation can come from any of its two states, and it can come from the state of the B component as well because the states of the parent and the child can work independently upon updates.

Therefore, we can conclude that to get the screen painted for the A component, we need to fix all props and states within itself and all its child components underneath. This is not a mathematical theory, but given the states from multiple components, this observation is apparent.

In short, props and states now both serve as the input of the component. The states can be especially vibrant since their values can be, but are not always, wired with an external system. The external system can be browser events or the API fetch, or anything else. Because a state can send to a child component via a prop, the effect of the state can cascade down deep into the app tree quickly.

You have been reading a chapter from
Designing React Hooks the Right Way
Published in: Jan 2022
Publisher: Packt
ISBN-13: 9781803235950
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