{"id":29436,"date":"2023-01-27T08:55:32","date_gmt":"2023-01-27T16:55:32","guid":{"rendered":"https:\/\/coderpad.io\/?p=29436"},"modified":"2023-06-07T13:38:28","modified_gmt":"2023-06-07T20:38:28","slug":"how-to-handle-state-management-in-vue-using-pinia","status":"publish","type":"post","link":"https:\/\/coderpad.io\/blog\/development\/how-to-handle-state-management-in-vue-using-pinia\/","title":{"rendered":"How To Handle State Management in Vue Using Pinia"},"content":{"rendered":"\n<p>State management is often necessary when building applications, but it can be challenging to implement, especially with large-scale applications.&nbsp;<\/p>\n\n\n\n<p>Your application&#8217;s size and features often dictate how large or complex your state management will be. Although keeping track of states in an application is not easy, it can be with the right tools for the job. This post aims to provide a high-level overview of one of the many ways in which state management can be handled in a Vue application using <a href=\"https:\/\/pinia.vuejs.org\/\" target=\"_blank\" rel=\"noopener\">Pinia<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is state management?<\/h2>\n\n\n\n<p>First, what is a state? State means data in your application, which ranges from data from a database server or an API response, to data from the client side, like the input values on a contact form. <strong>State simply means data that exists in our application.&nbsp;<\/strong><\/p>\n\n\n\n<p>As such, state management involves the process of creating, using, mutating, and sharing data across an application via components. In most cases, it relates to how user input or server data is handled. For instance, by filling out a form on a website, a user is adding new data\/state to the site or mutating an already existing state.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">When do we need state management?<\/h2>\n\n\n\n<p>State management is often necessary for complex or large-scale applications, where multiple components or modules must share and coordinate their state to function properly.&nbsp;<\/p>\n\n\n\n<p>State management becomes crucial when an application needs to remember data across multiple pages and components or when the application needs to keep track of user interactions. For example, a shopping cart application needs to remember what items have been added to the cart, and a web application needs to remember user preferences and settings. Without proper state management, applications would not be able to persist data and would then not be able to provide the expected functionality or user experience.<\/p>\n\n\n\n<p>However, when managing complex states, relying on just the internal state management offered by a framework such as Vue may not always be sufficient. It can sometimes lead to something called <em>props drilling<\/em>, a process of passing state down several unrelated components (that do not need it) to get said state to a deeply nested component that requires it.&nbsp;<\/p>\n\n\n\n<p>This makes it more challenging to keep track of the state and to identify which components require the state because most of these components are simply &#8220;passage&#8221; points for the state to reach the component that needs it. As a result, this approach can result in confusion, especially in large-scale applications with hundreds of states.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Different options for state management in Vue<\/h2>\n\n\n\n<p>State management is an essential topic in the frontend world, and the tools used depend on the particular framework being used.&nbsp;<\/p>\n\n\n\n<p>Like other front-end JavaScript frameworks, Vue offers several options for managing a web application&#8217;s state, and each has its advantages and disadvantages. The appropriate choice will depend on the specific needs of the application.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Vuex<\/h3>\n\n\n\n<p><a href=\"https:\/\/vuex.vuejs.org\/\" target=\"_blank\" rel=\"noopener\">Vuex<\/a> is the state management library created and maintained by the Vue team. It is a very complex yet powerful state management library that uses the <a href=\"https:\/\/facebook.github.io\/flux\/docs\/in-depth-overview\/\" target=\"_blank\" rel=\"noopener\">Flux design pattern<\/a> which Facebook built. In the same way as Redux, the Vuex state management library uses unidirectional data flow to pass data between components, ensuring that the state can be mutated predictably.&nbsp;<\/p>\n\n\n\n<p>It does this by providing a centralized store for storing and mutating application states, which serves as the only source of truth for all the components in a Vue application.&nbsp;<\/p>\n\n\n\n<p>However, using mutations with Vuex is one of the things that gets tiresome. Changing the state of our store from a component requires us first to call an action, which then triggers the mutation. As a result, each state change requires a great deal of boilerplate and extremely verbose code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Pinia<\/h3>\n\n\n\n<p>Pinia is a relatively new state management library for Vue.js that aims to provide a more lightweight and simplified state management alternative to VueX. It uses a similar approach to VueX but with a more modular and customizable design.&nbsp;<\/p>\n\n\n\n<p>Pinia was first introduced to the Vue ecosystem by a member of the core Vue 3 team as an experiment, but it quickly gained popularity and has become the official state management library recommended by the Vue team for Vue 3. The library is lightweight (1KB), straightforward to use, and does away with most of the complexity many developers dislike about the Vuex library.<\/p>\n\n\n\n<p>In Pinia, states are defined by creating stores containing states, getters, and actions. Essentially, the store&#8217;s state defines what global data is managed, getters return computed or derived values from the state (or other getters), and actions are methods that perform asynchronous operations or execute business logic.&nbsp;<\/p>\n\n\n\n<p>Pinia is relatively flexible as it can be used with the Composition API or Options API and allows developers to create plugins that can do a wide variety of things, like providing reusable functionality for stores. It also has strong support for Typescript, server-side-rendering (SSR), and hooks into Vue DevTools to facilitate easy debugging.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Other options<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/davestewart\/vue-class-store\" target=\"_blank\" rel=\"noopener\">Vue Class Store<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/harlemjs.com\/\" target=\"_blank\" rel=\"noopener\">Harlem<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">VueX vs Pinia<\/h2>\n\n\n\n<p>When comparing the two libraries, there are a few important advantages Pinia has over VueX:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Pinia&#8217;s API is much simpler than VueX\u2019s, and if you&#8217;ve worked with composeables in Vue3, Pinia will feel very familiar to you.<\/li>\n\n\n\n<li>You can only have one store in Vuex, whereas in Pinia, we can have multiple stores. In VueX, we <em>can<\/em> divide our store into modules if we need multiple stores. However, this is extremely tedious and requires us to import these modules into our components.<\/li>\n\n\n\n<li>Unlike Vuex where actions are dispatched via dispatch\/commit methods or MapAction\/MapMutation helper functions, Pinia stores dispatch actions as regular function calls.<\/li>\n\n\n\n<li>Vuex and Pinia both integrate well with Vue Devtools, so we can take full advantage of all of its debugging functionality.&nbsp;<\/li>\n\n\n\n<li>Pinia fully supports TypeScript out of the box, whereas Vuex does not have proper support, making it less suitable for TypeScript developers.&nbsp;<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a simple store with Pinia<\/h2>\n\n\n\n<p>Now that we have grasped Pinia and what it brings to the table, it is time to see a practical example of how it is used in a Vue application, as this will give us a better understanding of how to use it. We&#8217;ll be using a <a href=\"https:\/\/app.coderpad.io\/sandbox?pt=vue\">Vue CoderPad sandbox<\/a> for this article.<\/p>\n\n\n\n<p>In the sandbox shell, install the <code>pinia<\/code> library.<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs shcb-wrap-lines\">npm i pinia<\/code><\/span><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Setting up a store in Pinia<\/h2>\n\n\n\n<p>Setting up a store in Pinia is very simple; first, we need to set up a folder structure that will help tell where the state and all its configurations live in the project folder. We will create a folder named <code>stores<\/code> in the <code>src<\/code> directory. As stated earlier, this will be the directory that houses all the state management logic.&nbsp;&nbsp;<\/p>\n\n\n\n<p>Next, we create a Pinia instance \u2013 this is just a way to inject Pinia into the Vue app. It tells Vue there is an external library we intend to use alongside Vue internals. Otherwise, Vue won&#8217;t know we have a state library.&nbsp;<\/p>\n\n\n\n<p>As the <code>main.js<\/code> file is the entry point of a Vue application (the same way <code>index.html<\/code> is the entry file of a website), we create an instance in this root directory, so a Pinia instance initializes once our app starts. So let&#8217;s create the Pinia instance by editing our <code>main.js<\/code> to the code below:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> { createApp } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'vue'<\/span>\n<span class=\"hljs-keyword\">import<\/span> { createPinia } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'pinia'<\/span> <span class=\"hljs-comment\">\/\/ Import the createPinia function from Pinia<\/span>\n<span class=\"hljs-keyword\">import<\/span> App <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/App.vue'<\/span>\n\n<span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">'.\/assets\/main.css'<\/span>\n\n<span class=\"hljs-keyword\">const<\/span> app = createApp(App)\n\napp.use(createPinia())  <span class=\"hljs-comment\">\/\/ Create an instance of Pinia and injected it into the app.<\/span>\n\napp.mount(<span class=\"hljs-string\">'#app'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Next, we&#8217;ll set up a Pinia store that manages a <code>count<\/code> state to give us a better understanding of how simple it is to set up a Pinia store. In the <code>stores<\/code> folder, create a file <code>counter.js<\/code> to store state logic relevant to the counter application. Add the following into it:&nbsp;<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> { defineStore } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"pinia\"<\/span>;\n\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">const<\/span> useCounterStore = defineStore(<span class=\"hljs-string\">\"counter\"<\/span>, {\n  <span class=\"hljs-attr\">state<\/span>: <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> ({ <span class=\"hljs-attr\">count<\/span>: <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">\"David\"<\/span> }),\n  <span class=\"hljs-attr\">getters<\/span>: {\n    <span class=\"hljs-attr\">doubleCount<\/span>: <span class=\"hljs-function\">(<span class=\"hljs-params\">state<\/span>) =&gt;<\/span> state.count * <span class=\"hljs-number\">2<\/span>,\n  },\n  <span class=\"hljs-attr\">actions<\/span>: {\n    increment() {\n      <span class=\"hljs-keyword\">this<\/span>.count++;\n    },\n    decrement() {\n      <span class=\"hljs-keyword\">this<\/span>.count--;\n    },\n    changeName() {\n      <span class=\"hljs-keyword\">this<\/span>.name = <span class=\"hljs-string\">\"Franklin\"<\/span>;\n    },\n \n  },\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Look confusing? Don&#8217;t worry. We&#8217;ll go through it and explain every line of code.&nbsp;<\/p>\n\n\n\n<p>Let&#8217;s start with the first line, which is the importation of a function <code>defineStore<\/code>. As the name implies, this is a function imported from the Pinia package that lets us create an instance of a store, and it takes two arguments (more on that later).<\/p>\n\n\n\n<p>Then, we created an export function that exports all the <code>defineStore<\/code> state logic so it can be invoked in any Vue component to use this counter store. The naming convention typically begins with &#8220;use&#8221; and ends with &#8220;store&#8221; to help distinguish a function that manages state logic from other utility functions in our Vue application.<\/p>\n\n\n\n<p>The first argument accepted by the <code>defineStore<\/code> function is the name of the store you are creating or defining; this can be any name of your choice. However, it must be a unique name because Pinia uses it to connect the store to the dev tools.&nbsp;<\/p>\n\n\n\n<p>Furthermore, ensuring the store&#8217;s name reflects what you are storing in the state is a good and recommended practice. Since we are storing a count state that increases and decreases, we called our store <code>counter<\/code>.<\/p>\n\n\n\n<p>The second argument passed to the <code>defineStore<\/code> function is of two kinds: a <em>setup function<\/em> or an <em>options object<\/em>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Options object<\/h3>\n\n\n\n<p>The <strong>options object<\/strong> is the method we&#8217;ve opted for and is similar to Vue&#8217;s <a href=\"https:\/\/vuejs.org\/api\/#options-api\" target=\"_blank\" rel=\"noopener\">Options API<\/a>. We define it by passing an object containing our store&#8217;s state, getters, and actions property, as shown in the code snippet above.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Setup function&nbsp;<\/h3>\n\n\n\n<p>The <strong>setup function<\/strong> is the alternative second argument that can be passed to the <code>defineStore<\/code> function and is very similar to Vue&#8217;s <a href=\"https:\/\/vuejs.org\/api\/#composition-api\" target=\"_blank\" rel=\"noopener\">Composition API<\/a>. We pass in a callback function that defines our store&#8217;s reactive state,&nbsp; properties, and methods. This callback function returns the items in the store that we wish to expose for use. To understand it properly, let&#8217;s convert the initial <em>options object<\/em> to the <em>setup function<\/em>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-comment\">\/\/ Options Object<\/span>\n<span class=\"hljs-keyword\">import<\/span> { defineStore } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"pinia\"<\/span>;\n\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">const<\/span> useCounterStore = defineStore(<span class=\"hljs-string\">\"counter\"<\/span>, {\n  <span class=\"hljs-attr\">state<\/span>: <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> ({ <span class=\"hljs-attr\">count<\/span>: <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">\"David\"<\/span> }),\n  <span class=\"hljs-attr\">getters<\/span>: {\n    <span class=\"hljs-attr\">doubleCount<\/span>: <span class=\"hljs-function\">(<span class=\"hljs-params\">state<\/span>) =&gt;<\/span> state.count * <span class=\"hljs-number\">2<\/span>,\n  },\n  <span class=\"hljs-attr\">actions<\/span>: {\n    increment() {\n      <span class=\"hljs-keyword\">this<\/span>.count++;\n    },\n    decrement() {\n      <span class=\"hljs-keyword\">this<\/span>.count--;\n    },\n    changeName() {\n      <span class=\"hljs-keyword\">this<\/span>.name = <span class=\"hljs-string\">\"Franklin\"<\/span>;\n    },\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-comment\">\/\/ Setup Function<\/span>\n<span class=\"hljs-keyword\">import<\/span> { defineStore } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"pinia\"<\/span>;\n\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">const<\/span> useCounterStore = defineStore(<span class=\"hljs-string\">'counter'<\/span>, () =&gt; {\n  <span class=\"hljs-keyword\">const<\/span> count = ref(<span class=\"hljs-number\">0<\/span>)\n  <span class=\"hljs-keyword\">const<\/span> name = ref(<span class=\"hljs-string\">'David'<\/span>)\n  <span class=\"hljs-keyword\">const<\/span> doubleCount = computed(<span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> count.value * <span class=\"hljs-number\">2<\/span>)\n  <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">increment<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n    count.value++\n  } <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">decrement<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n    count.value--\n  }\n  <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">changeName<\/span> (<span class=\"hljs-params\"><\/span>) <\/span>{\n    name.value = <span class=\"hljs-string\">\"Franklin\"<\/span>\n}\n\n  <span class=\"hljs-keyword\">return<\/span> { count, name, doubleCount, increment, decrement, changeName }\n})<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Beautiful, isn&#8217;t it? I love how Pinia is flexible and allows us to use whichever method seems convenient. Moving forward, we will continue using the <em>options object<\/em> because it is straightforward and easy to use. You can use whatever you like in the future as long as you understand the fundamentals.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Importing the store into our Vue project<\/h2>\n\n\n\n<p>We will now see how to use this counter store in our component. Update the <code>App.vue<\/code> component to the code snippet below:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">setup<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">template<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">main<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span>&gt;<\/span>Hello World<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">main<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">template<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span> <span class=\"hljs-attr\">scoped<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The next step is to import and use the Pinia store we created earlier into our Vue project. We do this by importing the <code>useCounterStore<\/code> function from the <code>stores<\/code> directory; recall that this folder contains our counter state, getters, and actions.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">setup<\/span>&gt;<\/span><span class=\"javascript\">\n<span class=\"hljs-keyword\">import<\/span> { useCounterStore } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\".\/stores\/counter\"<\/span>;\n<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">template<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">main<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span>&gt;<\/span>Hello World<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">main<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">template<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span> <span class=\"hljs-attr\">scoped<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">How to access the state in our Vue component<\/h2>\n\n\n\n<p>After importing the function that contains our store, it is time to use it to make our Vue component interactive and stateful. All we have to do is destructure the items we need from the <code>useCounterStore()<\/code> function. However, to keep reactivity while destructuring reactive properties such as state and getters, we can use the <code>storeToRefs()<\/code> function. This creates refs for the reactive properties, while unreactive items can be destructured normally, as shown below.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">setup<\/span>&gt;<\/span><span class=\"javascript\">\n<span class=\"hljs-keyword\">import<\/span> { useCounterStore } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\".\/stores\/counter\"<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { storeToRefs } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"pinia\"<\/span>;\n\n<span class=\"hljs-keyword\">const<\/span> { count, name } = storeToRefs(useCounterStore()); <span class=\"hljs-comment\">\/\/destructuring reactive items from the store<\/span>\n<span class=\"hljs-keyword\">const<\/span> { increment, decrement } = useCounterStore(); <span class=\"hljs-comment\">\/\/destructuring unreactive item from the store<\/span>\n<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">template<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">main<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>Hey there, my name is {{ name }}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>Count: {{ count }}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">main<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">template<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span> <span class=\"hljs-attr\">scoped<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In a Vue application, you can think of states and getters as equivalent to data and computed values, respectively. For this reason, they are reactive values, while actions are regular functions that update the store.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/01\/img_63cdbfb45f9cc.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">A webpage with the text &#8220;Hey there, my name is David&#8221; and a count of 0.<\/figcaption><\/figure>\n\n\n\n<p>Now that we have that out of the way, let\u2019s create two buttons to increase and decrease the count value in our store. Recall we already created an increment and decrement action in our store that increases and decreases the current count value by 1, respectively, as shown below.&nbsp;&nbsp;<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">actions: {\n    increment() {\n      <span class=\"hljs-keyword\">this<\/span>.count++;\n    },\n    changeName() {\n      <span class=\"hljs-keyword\">this<\/span>.name = <span class=\"hljs-string\">\"Franklin\"<\/span>;\n    },\n    decrement() {\n      <span class=\"hljs-keyword\">this<\/span>.count--;\n    },\n  },<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now let&#8217;s hook these actions into our Vue component by attaching them to click events placed on the increase and decrease buttons; we&#8217;ll also add a bit of styling so they look presentable.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">setup<\/span>&gt;<\/span><span class=\"javascript\">\n<span class=\"hljs-keyword\">import<\/span> { useCounterStore } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\".\/stores\/counter\"<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { storeToRefs } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"pinia\"<\/span>;\n\n<span class=\"hljs-keyword\">const<\/span> { count, name } = storeToRefs(useCounterStore());\n<span class=\"hljs-keyword\">const<\/span> { increment, decrement } = useCounterStore();\n<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">template<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">main<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>Hey there, my name is {{ name }}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>Count: {{ count }}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"increase\"<\/span> @<span class=\"hljs-attr\">click<\/span>=<span class=\"hljs-string\">\"increment()\"<\/span>&gt;<\/span>Increase +<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"decrease\"<\/span> @<span class=\"hljs-attr\">click<\/span>=<span class=\"hljs-string\">\"decrement()\"<\/span>&gt;<\/span>Decrease -<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">main<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">template<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span> <span class=\"hljs-attr\">scoped<\/span>&gt;<\/span><span class=\"css\">\n<span class=\"hljs-selector-tag\">p<\/span> {\n  <span class=\"hljs-attribute\">margin-bottom<\/span>: <span class=\"hljs-number\">20px<\/span>;\n  <span class=\"hljs-attribute\">font-size<\/span>: <span class=\"hljs-number\">20px<\/span>;\n}\n<span class=\"hljs-selector-tag\">button<\/span> {\n  <span class=\"hljs-attribute\">border<\/span>: none;\n  <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">10px<\/span> <span class=\"hljs-number\">20px<\/span>;\n  <span class=\"hljs-attribute\">cursor<\/span>: pointer;\n  <span class=\"hljs-attribute\">margin-left<\/span>: <span class=\"hljs-number\">20px<\/span>;\n  <span class=\"hljs-attribute\">border-radius<\/span>: <span class=\"hljs-number\">5px<\/span>;\n  <span class=\"hljs-attribute\">color<\/span>: white;\n}\n\n<span class=\"hljs-selector-tag\">button<\/span><span class=\"hljs-selector-class\">.increase<\/span> {\n  <span class=\"hljs-attribute\">background-color<\/span>: green;\n}\n\n<span class=\"hljs-selector-tag\">button<\/span><span class=\"hljs-selector-class\">.decrease<\/span> {\n  <span class=\"hljs-attribute\">background-color<\/span>: red;\n}\n<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>From the code snippet above, we listen for click events on the increase and decrease buttons. When clicked, we fire the increment or decrement action from our store, which increases or decreases the store&#8217;s count state and reflects on the app as the count&#8217;s value changes.<\/p>\n\n\n\n<p>Output (after clicking the increase button 5 times):<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/01\/img_63cdbfb5d5590.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">The counter shows a value of 5 after the <strong>Increase<\/strong> button is clicked five times.<\/figcaption><\/figure>\n\n\n\n<p>From the code snippets above, we can see how easy it is to use a Pinia store in our Vue components. Next, let\u2019s see how we can use the <code>doubleCount<\/code> getter function, which multiplies the state by 2 and returns the computed value.&nbsp;<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">setup<\/span>&gt;<\/span><span class=\"javascript\">\n<span class=\"hljs-keyword\">import<\/span> { useCounterStore } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\".\/stores\/counter\"<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { storeToRefs } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"pinia\"<\/span>;\n\n<span class=\"hljs-keyword\">const<\/span> { count, name, doubleCount } = storeToRefs(useCounterStore());\n<span class=\"hljs-keyword\">const<\/span> { increment, decrement } = useCounterStore();\n<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">template<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">main<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>Hey there, my name is {{ name }}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>Count x 2: {{ doubleCount }}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>Count: {{ count }}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"increase\"<\/span> @<span class=\"hljs-attr\">click<\/span>=<span class=\"hljs-string\">\"increment()\"<\/span>&gt;<\/span>Increase +<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"decrease\"<\/span> @<span class=\"hljs-attr\">click<\/span>=<span class=\"hljs-string\">\"decrement()\"<\/span>&gt;<\/span>Decrease -<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">main<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">template<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span> <span class=\"hljs-attr\">scoped<\/span>&gt;<\/span><span class=\"css\">\n<span class=\"hljs-selector-tag\">p<\/span> {\n  <span class=\"hljs-attribute\">margin-bottom<\/span>: <span class=\"hljs-number\">20px<\/span>;\n  <span class=\"hljs-attribute\">font-size<\/span>: <span class=\"hljs-number\">20px<\/span>;\n}\n<span class=\"hljs-selector-tag\">button<\/span> {\n  <span class=\"hljs-attribute\">border<\/span>: none;\n  <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">10px<\/span> <span class=\"hljs-number\">20px<\/span>;\n  <span class=\"hljs-attribute\">cursor<\/span>: pointer;\n  <span class=\"hljs-attribute\">margin-left<\/span>: <span class=\"hljs-number\">20px<\/span>;\n  <span class=\"hljs-attribute\">border-radius<\/span>: <span class=\"hljs-number\">5px<\/span>;\n  <span class=\"hljs-attribute\">color<\/span>: white;\n}\n\n<span class=\"hljs-selector-tag\">button<\/span><span class=\"hljs-selector-class\">.increase<\/span> {\n  <span class=\"hljs-attribute\">background-color<\/span>: green;\n}\n\n<span class=\"hljs-selector-tag\">button<\/span><span class=\"hljs-selector-class\">.decrease<\/span> {\n  <span class=\"hljs-attribute\">background-color<\/span>: red;\n}\n<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The first thing we did was import the <code>doubleCount<\/code> getters method from the store using the <code>storeToRefs<\/code> function to maintain its reactivity in our component. Now when the count state is 4, <code>doubleCount<\/code>&#8216;s value is computed to 8, and when the count state is 7, <code>doubleCount<\/code>&#8216;s value is computed to 14 as shown below<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/01\/img_63cdbfb748668.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">The count value is doubled once the <strong>increase<\/strong> button is clicked,<\/figcaption><\/figure>\n\n\n\n<p>Getters can be used for far more complex and practical uses than multiplying a count by 2 \u2013 for example, to build a cart system in Vue using Pinia as our state management library.&nbsp;<\/p>\n\n\n\n<p>We can use getters to calculate the prices of items in the cart and even add tax, discount, and delivery fee so that customers can instantly see the total price of the goods, which gets updated as items are added or removed from the cart. Getters are the best solution for such use cases.<\/p>\n\n\n\n<p>Finally, we&#8217;ll add a button to fire the <code>changeName<\/code> action in our store so the name property changes from &#8220;David&#8221; to &#8220;Franklin&#8221; when this button is clicked.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">setup<\/span>&gt;<\/span><span class=\"javascript\">\n<span class=\"hljs-keyword\">import<\/span> { useCounterStore } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\".\/stores\/counter\"<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { storeToRefs } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"pinia\"<\/span>;\n\n<span class=\"hljs-keyword\">const<\/span> { count, name, doubleCount } = storeToRefs(useCounterStore());\n<span class=\"hljs-keyword\">const<\/span> { increment, decrement, changeName } = useCounterStore();\n<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">template<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">main<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>Hey there, my name is {{ name }}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>Count x 2: {{ doubleCount }}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>Count: {{ count }}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"increase\"<\/span> @<span class=\"hljs-attr\">click<\/span>=<span class=\"hljs-string\">\"increment()\"<\/span>&gt;<\/span>Increase +<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"decrease\"<\/span> @<span class=\"hljs-attr\">click<\/span>=<span class=\"hljs-string\">\"decrement()\"<\/span>&gt;<\/span>Decrease -<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> @<span class=\"hljs-attr\">click<\/span>=<span class=\"hljs-string\">\"changeName()\"<\/span>&gt;<\/span>Change Name<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">main<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">template<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span> <span class=\"hljs-attr\">scoped<\/span>&gt;<\/span><span class=\"css\">\n<span class=\"hljs-selector-tag\">p<\/span> {\n  <span class=\"hljs-attribute\">margin-bottom<\/span>: <span class=\"hljs-number\">20px<\/span>;\n  <span class=\"hljs-attribute\">font-size<\/span>: <span class=\"hljs-number\">20px<\/span>;\n}\n<span class=\"hljs-selector-tag\">button<\/span> {\n  <span class=\"hljs-attribute\">border<\/span>: none;\n  <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">10px<\/span> <span class=\"hljs-number\">20px<\/span>;\n  <span class=\"hljs-attribute\">cursor<\/span>: pointer;\n  <span class=\"hljs-attribute\">margin-left<\/span>: <span class=\"hljs-number\">20px<\/span>;\n  <span class=\"hljs-attribute\">border-radius<\/span>: <span class=\"hljs-number\">5px<\/span>;\n  <span class=\"hljs-attribute\">color<\/span>: white;\n  <span class=\"hljs-attribute\">background-color<\/span>: aqua;\n}\n\n<span class=\"hljs-selector-tag\">button<\/span><span class=\"hljs-selector-class\">.increase<\/span> {\n  <span class=\"hljs-attribute\">background-color<\/span>: green;\n}\n\n<span class=\"hljs-selector-tag\">button<\/span><span class=\"hljs-selector-class\">.decrease<\/span> {\n  <span class=\"hljs-attribute\">background-color<\/span>: red;\n}\n<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Again, we first imported the <code>changeName<\/code> action from our counter store, and then we hooked it up to the button so that when the button is clicked, the name value is changed from &#8220;David&#8221; to &#8220;Franklin.&#8221;&nbsp;<\/p>\n\n\n\n<p>This is the current state when the button is not clicked yet:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/01\/img_63cdbfb8822f5.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">The application in default state with the name &#8216;David&#8217;.<\/figcaption><\/figure>\n\n\n\n<p>Now, let\u2019s click the &#8220;Change Name&#8221; button to change the name.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/01\/img_63cdbfb980d9e.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Once the &#8220;Change Name&#8221; button is clicked, the name state is changed to &#8216;Franklin&#8217;.<\/figcaption><\/figure>\n\n\n\n<p>With that, you&#8217;re now ready to use Pinia as you like.<\/p>\n\n\n\n<p>Play with the sandbox to understand how state is managed by Pinia:<\/p>\n\n\n<div\n\tclass=\"sandbox-embed responsive-embed  sandbox-embed--full-width\"\n\tstyle=\"padding-top: 85%\"\ndata-block-name=\"coderpad-sandbox-embed\">\n\t<iframe src=\"https:\/\/embed.coderpad.io\/sandbox?question_id=240283&#038;use_question_button\" width=\"640\" height=\"544\" loading=\"lazy\" aria-label=\"Try out the CoderPad sandbox\"><\/iframe>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Congratulations on getting to this article&#8217;s end; we have learned what state management is, explored how Pinia differs from VueX, and how to install and set up a store in Pinia. We also looked at various examples of how to use Pinia. You can read more about Pinia in the <a href=\"https:\/\/pinia.vuejs.org\/\" target=\"_blank\" rel=\"noopener\">official docs<\/a>. I hope you found this article helpful and enjoyed it.&nbsp;<\/p>\n\n\n\n<p><em><a href=\"https:\/\/twitter.com\/daveyhert\" target=\"_blank\" rel=\"noopener\">David<\/a> is a front-end developer by day and a technical writer by night, who enjoys breaking down complex topics into comprehensible bits digestible to even 5-year-olds.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>State management is often necessary when building applications, but it can be challenging to implement, especially with large-scale applications. This blog post will teach you how to handle state management in Vue using Pinia.<\/p>\n","protected":false},"author":1,"featured_media":29586,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9],"tags":[],"persona":[29],"blog-programming-language":[64],"keyword-cluster":[],"class_list":["post-29436","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"_links":{"self":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/29436","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/comments?post=29436"}],"version-history":[{"count":51,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/29436\/revisions"}],"predecessor-version":[{"id":34550,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/29436\/revisions\/34550"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media\/29586"}],"wp:attachment":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media?parent=29436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/categories?post=29436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/tags?post=29436"},{"taxonomy":"persona","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/persona?post=29436"},{"taxonomy":"blog-programming-language","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/blog-programming-language?post=29436"},{"taxonomy":"keyword-cluster","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/keyword-cluster?post=29436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}