In this episode, we dive into the world of composables in Vue and why they're so useful for managing reusable functionality and shared state across your components—without needing to pull in a big state management library. If you're already super comfortable with composables, you might want to skip ahead, but for everyone else, we're going over the basics and building a practical example together.
We start by cleaning up some old components and then set up a new navigation component that will display the current user's name. Next, we create a new composable called useAuth
—this will hold our authentication logic and state. You’ll see how we can use Vue’s reactivity to store the user’s name and provide functions to update or retrieve it.
We connect this useAuth
composable in our Home view to set the username (pretending to "log in" a user), and read from it in our Navigation component so that any updates are instantly reflected. This demonstrates simple global state management using just composables—no Vuex or Pinia required.
By the end, you'll see how the pieces fit together: setting state from one component and reflecting changes in another. This technique is perfect for basic authentication or any shared information you want to access and update from different parts of your Vue app.