The Art of State Management in Web Development
Each type of state has its own use cases, and when you understand them, you can ensure seamless state management across your applications. Let's dive in.
Jacob Kofoed
January 22, 2024
1. Local State
2. Inherited State (Context)
3. Global State
4. Local Storage
5. Session Storage
6. URL Parameters
7. Server State
-
Invalidate APIs: After a request has been sent, we tell all our dependent client-side APIs that data may have changed. This comes with a small delay as we have to do a TCP from CLIENT -> SERVER -> CLIENT, but we won't have to write additional logic to keep the state on the server and client. -
Optimistic update: Assume the request went fine and immediately update some state on the client side. This requires us to keep a copy of the state client-side. -
Socket connection: If multiple users can modify the same data, we will need something that resembles live data, and the closest we can get is when we set up a Web Socket for dual-directional data. For even faster performance, you can combine a socket connection with optimistic updates.
Conclusion
A note on complexity