<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>store Archives - Program Easily</title>
	<atom:link href="https://programeasily.com/tag/store/feed/" rel="self" type="application/rss+xml" />
	<link>https://programeasily.com/tag/store/</link>
	<description>Program Easily helps people to learn about software programs in a easy manner.</description>
	<lastBuildDate>Tue, 23 Aug 2022 04:14:22 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.1</generator>

<image>
	<url>https://i0.wp.com/programeasily.com/wp-content/uploads/2020/12/cropped-logo.png?fit=32%2C32&#038;ssl=1</url>
	<title>store Archives - Program Easily</title>
	<link>https://programeasily.com/tag/store/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">187075990</site>	<item>
		<title>Pinia &#8211; The Vue Store Library</title>
		<link>https://programeasily.com/2022/08/23/pinia-the-vue-store-library/</link>
					<comments>https://programeasily.com/2022/08/23/pinia-the-vue-store-library/#respond</comments>
		
		<dc:creator><![CDATA[Admin]]></dc:creator>
		<pubDate>Tue, 23 Aug 2022 04:14:22 +0000</pubDate>
				<category><![CDATA[Vuejs]]></category>
		<category><![CDATA[pinia]]></category>
		<category><![CDATA[store]]></category>
		<category><![CDATA[vue]]></category>
		<category><![CDATA[vue3]]></category>
		<guid isPermaLink="false">https://programeasily.com/?p=688</guid>

					<description><![CDATA[<p>What is Pinia Pinia is a library store for Vue.js. It will work with Vue.js 3 as well as Vue.js 2. Pinia holds the state and business logic which is not bounded with component object trees. In other words, I would say it creates a global state where everybody can read and write the data. Pinia has three area&#8217;s as follows, State Getters Action We can assume and compare these things with data, computed and methods in components. Why Pinia...</p>
<p class="read-more"><a class="btn btn-default" href="https://programeasily.com/2022/08/23/pinia-the-vue-store-library/"> Read More<span class="screen-reader-text">  Read More</span></a></p>
<p>The post <a rel="nofollow" href="https://programeasily.com/2022/08/23/pinia-the-vue-store-library/">Pinia &#8211; The Vue Store Library</a> appeared first on <a rel="nofollow" href="https://programeasily.com">Program Easily</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h2>What is Pinia</h2>
<p><span style="font-weight: 400;"><a href="https://pinia.vuejs.org/">Pinia</a> is a library store for <a href="https://programeasily.com/vue/">Vue.js</a>. It will work with Vue.js 3 as well as Vue.js 2. Pinia holds the state and business logic which is not bounded with component object trees. In other words, <strong>I would say it creates a global state where everybody can read and write the data</strong>. Pinia has three area&#8217;s as follows,</span></p>
<ol>
<li>State</li>
<li>Getters</li>
<li>Action</li>
</ol>
<p><span style="font-weight: 400;">We can assume and compare these things with data, computed and methods in components.</span></p>
<p><img fetchpriority="high" decoding="async" class="aligncenter wp-image-1625 size-full" src="https://i0.wp.com/programeasily.com/wp-content/uploads/2022/08/Snapshot_13.png?resize=640%2C360&#038;ssl=1" alt="Pinia Introduction" width="640" height="360" srcset="https://i0.wp.com/programeasily.com/wp-content/uploads/2022/08/Snapshot_13.png?w=1920&amp;ssl=1 1920w, https://i0.wp.com/programeasily.com/wp-content/uploads/2022/08/Snapshot_13.png?resize=300%2C169&amp;ssl=1 300w, https://i0.wp.com/programeasily.com/wp-content/uploads/2022/08/Snapshot_13.png?resize=1024%2C576&amp;ssl=1 1024w, https://i0.wp.com/programeasily.com/wp-content/uploads/2022/08/Snapshot_13.png?resize=768%2C432&amp;ssl=1 768w, https://i0.wp.com/programeasily.com/wp-content/uploads/2022/08/Snapshot_13.png?resize=1536%2C864&amp;ssl=1 1536w, https://i0.wp.com/programeasily.com/wp-content/uploads/2022/08/Snapshot_13.png?resize=480%2C270&amp;ssl=1 480w, https://i0.wp.com/programeasily.com/wp-content/uploads/2022/08/Snapshot_13.png?w=1280&amp;ssl=1 1280w" sizes="(max-width: 640px) 100vw, 640px" data-recalc-dims="1" /></p>
<h2 id="when-should-i-use-a-store" tabindex="-1">Why Pinia (A Store)</h2>
<p><span style="font-weight: 400;"><strong>Store will have the data that can be used everywhere in our application</strong>. Meanwhile we should avoid the local data in the store which can be added in the component instead. Pinia is a choice if your application needs to access the global state values.</span></p>
<h2>Features of Pinia</h2>
<ul>
<li>Hot module replacement
<ul>
<li>Modify your stores without reloading your page</li>
<li>Keep any existing state while developing</li>
</ul>
</li>
<li>Plugins: extend Pinia features with plugins</li>
<li>Proper TypeScript support or <strong>autocompletion</strong> for JS users</li>
<li>Server Side Rendering Support</li>
<li>Devtools support
<ul>
<li>A timeline to track actions, mutations</li>
<li>Stores appear in components where they are used</li>
<li>Time travel and easier debugging</li>
</ul>
</li>
</ul>
<h2>Full Tutorial</h2>
<h3>Installations</h3>
<p>You can install pinia with the help of NPM or Yarn as follows,</p>
<pre><code class="language-bash">npm install pinia
or
yarn add pinia</code></pre>
<h3>Inject Pinia with CreateApp</h3>
<p><span style="font-weight: 400;">Here we are going to have only one store for the application. So we have to create its instance and inject the same in VUE createApp. You can call it a root store.</span></p>
<pre><code class="language-javascript">import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'

const pinia = createPinia()
const app = createApp(App)

app.use(pinia)
app.mount('#app')</code></pre>
<p><span style="font-weight: 400;">So what createPinia does here ?. It creates the entity which holds the state for our application. And It&#8217;s global state where one can read and write as well. It has three concepts or property&#8217;s</span></p>
<ol>
<li>state</li>
<li>getters</li>
<li>actions</li>
</ol>
<p>(You can assume this three components with data, computed and methods)</p>
<h3>Create the Store</h3>
<p><span style="font-weight: 400;">First we need to create the store template. We can create a store template using defineStore() and it requires a unique name. We may use this template across our application.</span></p>
<pre><code class="language-javascript">import { defineStore } from 'pinia'

// useStore could be anything like useUser, useCart
// the first argument is a unique id of the store across your application
export const useStore = defineStore('main', {
  // other options...
})</code></pre>
<p><span style="font-weight: 400;">Second we need to use this store template inside the setup() function so that it will create the store. We have to call useStore() as follows,</span></p>
<pre><code class="language-javascript">import { useStore } from '@/stores/counter'

export default {
  setup() {
    const store = useStore()

    return {
      // you can return the whole store instance to use it in the template
      store,
    }
  },
}</code></pre>
<p><span style="font-weight: 400;">Once the store is created you can access its properties like state, getters and actions.</span></p>
<h3>State</h3>
<p>The state is the heart of the system. It is a function that returns the initial state of the application.</p>
<pre><code class="language-javascript">import { defineStore } from 'pinia'

const useStore = defineStore('storeId', {
  // arrow function recommended for full type inference
  state: () =&gt; {
    return {
      // all these properties will have their type inferred automatically
      counter: 0,
      name: 'Eduardo',
      isAdmin: true,
    }
  },
})</code></pre>
<p><span style="font-weight: 400;">We can access the store to read and write its data through store instance, and we can reset it to the initial state as well</span></p>
<pre><code class="language-javascript">const store = useStore()

store.counter++

store.$reset()</code></pre>
<h3>Getter</h3>
<p><span style="font-weight: 400;">We can define the getters property in defineStore() template. I would say, it is a replacement for computed values.</span></p>
<pre><code class="language-javascript">export const useStore = defineStore('main', {
  state: () =&gt; ({
    counter: 0,
  }),
  getters: {
    // automatically infers the return type as a number
    doubleCount(state) {
      return state.counter * 2
    },
    // the return type **must** be explicitly set
    doublePlusOne(): number {
      // autocompletion and typings for the whole store 
      return this.doubleCount + 1
    },
  },
})</code></pre>
<p><span style="font-weight: 400;">Then we can access and use the getters inside the template as follows,</span></p>
<pre><code class="language-javascript">&lt;template&gt;
  &lt;p&gt;Double count is {{ store.doubleCount }}&lt;/p&gt;
&lt;/template&gt;

&lt;script&gt;
export default {
  setup() {
    const store = useStore()

    return { store }
  },
}
&lt;/script&gt;</code></pre>
<h3>Actions</h3>
<p><span style="font-weight: 400;">Actions are the replacement for the methods. We can add our actions in the defineStore() template. Unlike getters, we can write </span><b>asynchronous </b><span style="font-weight: 400;">operations inside action. It will useful for creating API calls for our business logic</span></p>
<pre><code class="language-javascript">import { mande } from 'mande'

const api = mande('/api/users')

export const useUsers = defineStore('users', {
  state: () =&gt; ({
    userData: null,
    // ...
  }),

  actions: {
    async registerUser(login, password) {
      try {
        this.userData = await api.post({ login, password })
        showTooltip(`Welcome back ${this.userData.name}!`)
      } catch (error) {
        showTooltip(error)
        // let the form component display the error
        return error
      }
    },
  },
})</code></pre>
<h2>Summary</h2>
<ul>
<li>Pinia is a store library for Vue, it allows you to share a state across components/pages.</li>
<li>Compared to Vuex, Pinia provides a simpler API with less ceremony, offers Composition-API-style APIs, and most importantly, has solid type inference support when used with TypeScript.</li>
<li>The formula is Create the template -&gt; create the store with state, getters and actions and use across the application.</li>
</ul>
<p>The post <a rel="nofollow" href="https://programeasily.com/2022/08/23/pinia-the-vue-store-library/">Pinia &#8211; The Vue Store Library</a> appeared first on <a rel="nofollow" href="https://programeasily.com">Program Easily</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://programeasily.com/2022/08/23/pinia-the-vue-store-library/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">688</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/

Page Caching using disk: enhanced 
Minified using disk
Database Caching 7/41 queries in 0.080 seconds using disk

Served from: programeasily.com @ 2025-06-20 07:48:10 by W3 Total Cache
-->