Proven Pinia Patterns

Rate this content
Bookmark

With Vue's new-and-improved state management library, Pinia, we gain a much more modular tool. While being more flexible, leaner, and lacking the Mutations of Vuex, Pinia presents us with more opportunities to be creative, for better or worse, with our app architecture and how state management is conducted and organized within it.

This talk explores some @posva-approved best practices and architectural design patterns to consider when using Pinia in production.

This talk has been presented at Vue.js London 2023, check out the latest edition of this JavaScript Conference.

FAQ

Vue Mastery is the ultimate learning resource for Vue developers, offering a collection of courses to help elevate coding skills and become proficient in Vue development.

Pinnia is a state management library for Vue applications, designed to be a lighter, more modular, and less prescriptive alternative to VueX, offering more freedom in implementing state management.

Pinnia provides consistent patterns for collaborative organization, SSR security, DevTools support, and first-class TypeScript support, making it ideal for large-scale applications and team collaboration.

Options stores use an object to define state, actions, and getters, similar to Vue's Options API. Setup stores, on the other hand, use a function, allowing direct usage of Vue 3's Composition API features like `ref` and `reactive` for defining state.

In Pinnia, state can be accessed and mutated directly within store actions or components. State properties can be accessed using `this` in options stores or directly in setup stores. Pinnia also supports state updates through actions or directly setting state values.

The patch method in Pinnia allows for bulk updates to the state by passing an object or function, simplifying state mutations and tracking them in DevTools. The reset method resets the store's state to its initial values, useful for scenarios like user logout.

Store organization in Pinnia involves grouping related data into their own stores based on logical concerns or features of the app. This modular approach enhances code clarity, collaboration, and efficiency in managing state.

Pinnia is designed to safely manage global state in server-side rendered (SSR) applications, preventing security vulnerabilities that arise when state is shared across requests on the server.

Vue Mastery offers a course titled 'Proven Pinnia Patterns' and other learning resources that cover extensive aspects of Pinnia, including its features, implementation patterns, and best practices in Vue applications.

Adam Jahr
Adam Jahr
20 min
15 May, 2023

Comments

Sign in or register to post your comment.

Video Summary and Transcription

Pinnia is a lighter, more modular state management solution for Vue apps, offering consistent patterns, TypeScript support, and an intuitive developer experience. PINIA encourages splitting state into manageable domains, improving code splitting, type inferences, team collaboration, and debugging clarity. Pinia provides flexibility in accessing and mutating state, with different approaches for option and setup stores. It also offers features like patch, reset, and onAction methods. Vue Mastery offers comprehensive Pinnia courses and resources for fast learning.
Available in Español: Patrones Probados de Pinia

1. Introduction to Pinnia and its Features

Short description:

Hello, I'm Adam Jarr, co-founder of Vue Mastery. Today, we'll explore Pinnia, a lighter, more modular state management solution for Vue apps. We'll cover organizing stores, options vs. setup stores, accessing and updating state, and unique Pinnia features like patch and reset. We'll use a demo app, a Restaurant Finder, to illustrate these concepts. Let's dive in!

Hello, my name is Adam Jarr. I am co-founder of Vue Mastery, the ultimate learning resource for Vue developers. Through our collection of Vue courses, you can level up your skills, elevate your code and become the best Vue developer you possibly can be.

So in today's talk, we're going to take a journey deep into the Vue ecosystem to explore the planet of Pinnia. As we should all be aware by now, Pinnia is the evolution of where VueX was, bringing a newer, lighter weight, more modular, less prescriptive, more freeing version of state management into our Vue apps. And as the saying goes, with great freedom comes great responsibility. And we have added responsibility now because we can get creative about how we implement state management in our Vue apps. And when we do this well, we can use Pinnia to develop very elegantly architected apps, avoiding anti-patterns, and creating an application whose state can scale as we scale.

And that brings us to the focus of today's talk, where we unpack the proven Pinnia patterns that you can use confidently throughout your apps, because this talk and its contents were approved by Posva, or Eduardo San Martín Marrote, the creator of Pinnia. So by the end of this talk, we will have covered organizing our Pinnia stores, options versus setup stores, how they differ, which one you might want to choose for what use case, accessing and updating your Pinnia state. And we'll also explore some unique Pinnia features such as patch and reset. Throughout this talk, we're going to be looking at a demo app so we can apply and unpack these features in a more real-world use case. And as you can see, this is a Restaurant Finder. You're going to type in the city and search Sherm to find restaurants within a certain area, and the user can register an account. They can save their favorites, and they can read information pulled from the Google Maps API, such as ratings and reviews. This app is going to be tracking each of its logical concerns globally with its own Pinnia stores, and we're going to cover all this throughout the talk, so let's get started.

2. Why Use Pinnia and Its Features

Short description:

Vue 3's composition API provides a basic state management solution, but Pinnia offers consistent patterns for collaborative organization, SSR security, DevTools for debugging, TypeScript support, and an intuitive developer experience. If you need these features, pinya is the way to go.

Before we dive deeper into Pinnia concepts, let's first get clear on why you would actually want to use Pinnia in the first place, because Vue 3 already has the composition API with a built-in reactivity system with the flexibility for sharing and reusing state. Just using the composition API, we could create a reactive object to serve as a store to manage our global state. Then we would just import that store into whichever component needs it, and because of how Vue's reactivity system works, any component that imports that store can directly mutate its global state. And if that state were to change and it had a template that were displaying that state, that template would reactively change as well.

So again, if we can just use the composition API to create a minimal, fundamental version of state management in a Vue 3 app, then when exactly would we need a state management library such as pinya? Well, it's going to come in handy when you want to have consistent patterns for collaborative organization. This is especially important for large teams that are collaborating on a large-scale application. One such pattern that you might want to follow collaboratively together is a predictable way for you to mutate state and the actions within pinya allow us to achieve that. SSR security is another thing to consider and why you might want to use pinya because when you're using server-side rendering, you'll need to be careful with how you manage global state since SSR apps, they initialize the application modules on the server and then share that state across every request. So this could lead to security vulnerabilities, but pinya was designed to make it safer and easier to manage state in those server-side rendered applications. Additionally, when using pinya, we get the DevTools and all the transparency and helpful insight that those tools can provide us. This is going to make debugging and really understanding the state of our applications a lot clearer. Increasingly, it's becoming important for JavaScript developers to be working with tools that have TypeScript support, and pinya has first-class support for using TypeScript. All of these reasons combined create for a smooth and intuitive developer experience when you're using pinya to manage state in your applications. So, if your needs include any of the things that I laid out in this list here, you're probably going to want to decide to use pinya instead of just relying on the Composition API for your state management needs. So all of that to say, when you use pinya, you're going to be empowered with a robust and refined way to manage the state in your application, architect it in a way that you can be able to debug and collaborate nicely amongst your team. So, now that we're clear on when exactly we would want to use pinya, let's start to explore those features.