useState and useReducer: These two react to module state.
In other words, when a button is clicked, useState's count reacts (increases or decreases), and the component re-renders.
π 3. Using Selectors and useSubscription
Purpose of Using Selectors
Using selectors allows components to select and use only the parts of state they need.
This prevents unnecessary re-rendering and optimizes performance.
Applying to Project
For example, as shown above, components use the useSubscription hook and Selector to subscribe only to the state they need.
My Thoughts
This chapter introduces the concept of module state and subscriptions, which is a different approach to state management compared to React's built-in state. The key insight is that we can manage state outside of React components and use subscriptions to keep components in sync with that external state.
The use of selectors is particularly important for performance optimization - it allows components to only re-render when the specific parts of state they care about change, rather than when any part of the global state changes.
This pattern is commonly used in state management libraries like Redux and Zustand, where the state exists outside of React but components can subscribe to changes.