Chapter 2: Using Local and Global States
React components form a tree structure. In the tree structure, creating a state in a whole subtree is straightforward; you would simply create a local state in a higher component in a tree and use the state in the component and its child components. This is good in terms of locality and reusability and is why it's generally recommended to follow this strategy.
However, in some scenarios, we have a state in two or more components that are far apart in the tree. In such cases, this is where global states come in. Unlike local states, global states do not conceptually belong to a specific component, and so where we store a global state is an important point to consider.
In this chapter, we will learn about local states, including some lifting-up patterns that may be worth considering. Lifting up is a technique to put information higher in the component tree. Then, we will dive into global states and consider when to use them.
We...