Part 6 introduces more advanced state management.
As an app grows, more components need to share the same data.
For example, login information, a shopping cart, and theme preferences should remain consistent throughout the app.
Trying to manage this global state only through props can make code complicated and untidy.
Vue provides its official state-management library,
Pinia
, to address this.
Pinia is a central store for data shared by components.
Any component can save and retrieve data there, making complex flows simpler and more intuitive.
A store groups related state, derived values called getters, and functions that modify state called actions.
Stores can be organized by feature, such as a counter or user authentication.
Create src/stores/counter.js and define a counter store.
The storeToRefs utility is important.
If you destructure directly with const { count } = counterStore, count becomes a plain number and loses reactivity.
storeToRefs solves this by wrapping a store's state and getters properties in reactive ref objects.
Pinia's strength is how easily components can share the same state.
Let's show the count in DisplayComponent, separate from the previously created CounterComponent.
Place both components in App.vue.
Pressing a button in CounterComponent also updates the number in DisplayComponent immediately.
That is the benefit of centralized state management.
React's built-in Context API also solves prop drilling, but large apps with frequent state changes may encounter performance issues.
Pinia is highly optimized for Vue's reactivity system.
Pinia vs. Recoil/Zustand
These are popular React state-management libraries.
While they focus on atomic state pieces, Pinia takes a structured approach that groups state, getters, and actions in one store.
The biggest difference is that Pinia is Vue's
official
library, providing close Vue integration and long-term support.
We learned to manage application-wide state using Pinia.
It makes complex component data flows simpler and clearer.
Instead of passing props through multiple levels, components can access the store directly where needed.
This significantly improves maintainability and extensibility.
Vue Basics, Part 7 covers API integration and asynchronous operations.