In this episode, we dive into the concept of single-value stores in Alpine. Sometimes, you don't need an entire store object with properties, methods, or getters—maybe you just need to track a simple value, like a user's name, a theme mode (dark or light), or a notification count. That's what single-value stores are for, and here you'll see exactly how to implement and use them.
We'll start by setting up a basic single-value store with a primitive value (like a string or number) instead of a complex object, and see how straightforward it is to display this value in the UI. You’ll watch us stumble over a typical gotcha (missing x-data
) and see how to quickly fix it.
To make things more interesting, we build out a practical example: dynamically updating the page title based on a notification count stored in a single-value store. We simulate an incoming event (like a new notification) with a timeout, updating the store and, in turn, the title bar. This is a great demonstration of how you can use Alpine’s stores to share and react to state changes from anywhere in your app.
By the end, you’ll see how single-value stores help keep things simple and tidy when you just need to manage one bit of state. If you only have a single value to track, no need for a big store object—just drop in a single-value store and you're good to go.