10. Use Case Scenario 4: React Tracked
Understanding React Tracked (using with useState, useReducer, React Redux)
π 1. What is React Tracked?
State Usage Tracking
React Tracked is a state usage tracking library that automatically performs rendering optimization based on property detection
It can be used with other state management libraries like Redux, Zustand, etc.
It differs from simple global state management libraries in that it provides rendering optimization functionality
Comparison with Context
When only a specific value is changed, new context values are propagated, and
useContextdetects re-rendering. Therefore, unnecessary re-rendering can occurWhen using React Tracked, you define a
useTrackedhook that can be used instead ofuseContext. This wraps state with proxy and tracks usage
Example Code
const useFirstName = () => {
const [{ firstName }] = useTracked();
return firstName;
};Characteristics Seen Through Examples
First, since the usage is the same as
useContext, it has the advantage of being easy to applyThe code is not much different from when using Context, but internally it tracks state usage and automatically optimizes rendering! A very clever fellow π
The methods of using with
useStateanduseReducerthat come later also mainly deal with content similar to the above
π 2. Pros and Cons of useTracked
useTrackedCharacteristics and Advantages of useTracked
useTrackedAs mentioned above, it tracks based on Proxy.
useTrackedwraps state objects with Proxy or uses similar techniques to intercept property (get) access. (Similar content also appeared in Chapter 9: Valtio) As a result, it can record which properties of that state object the component actually read (accessed)Only accessed properties trigger re-rendering. When rendering ends,
useTrackedrecognizes "This component depends on properties A, B, C" and only tracks those properties. After that, if any of A, B, C changes, it re-renders the component using that hook. On the other hand, even if unused properties change, this component is not re-rendered. (Very efficient)It performs re-rendering optimization. Since only truly necessary fields from global state or large objects are considered "actual dependencies," unnecessary renders can be greatly reduced. For example, if only
user.namewas accessed, the component won't re-render even ifuser.ageoruser.emailchanges
Disadvantages of useTracked
useTrackedThere can be performance overhead in the process of recording which properties were read in each rendering process and detecting property access through Proxy. It might not be a big problem in general situations, but it would be good to be careful in cases with complex data structures or frequent rendering
The more deeply nested the state structure is, the more tracking logic there will be. For example, in cases where you need to track properties that go deep like
obj.a.b.c, unexpected performance costs or unexpected re-rendering might occur. (But this doesn't seem to be only a disadvantage ofuseTracked)Teams familiar with explicit action/reducer structures like Redux might feel that the automatic tracking provided by
useTrackedis abstract. Since state change flow is not clearly visible, state tracking/debugging might be difficult
π References
My Thoughts
This chapter introduces React Tracked, which provides automatic optimization by tracking which properties of state are actually used by components. The key insight is that React Tracked can be used as a drop-in replacement for useContext while providing much better performance.
The discussion about the pros and cons of useTracked is particularly valuable - it shows that while the automatic tracking is powerful, it comes with some trade-offs in terms of performance overhead and debugging complexity.
The comparison with Context is important because it shows how React Tracked solves a real problem that many developers face when using React Context for state management.
Code Examples
Last updated