]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
docs: add HMR entry
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 28 Jul 2021 13:49:54 +0000 (15:49 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 28 Jul 2021 13:49:54 +0000 (15:49 +0200)
docs/.vitepress/config.js
docs/cookbook/hot-module-replacement.md [new file with mode: 0644]
docs/cookbook/index.md

index f2f4076f8e11a762a4b863ec1bae9a06c708ceed..ef1566c1a1d95a04d28057858f250b999444b75a 100644 (file)
@@ -216,6 +216,10 @@ module.exports = {
           text: 'Cookbook',
           link: '/cookbook/',
           children: [
+            {
+              text: 'Hot Module Replacement',
+              link: '/cookbook/hot-module-replacement.html',
+            },
             {
               text: 'Testing',
               link: '/cookbook/testing.html',
diff --git a/docs/cookbook/hot-module-replacement.md b/docs/cookbook/hot-module-replacement.md
new file mode 100644 (file)
index 0000000..0f32c67
--- /dev/null
@@ -0,0 +1,20 @@
+# HMR (Hot Module Replacement)
+
+Pinia supports Hot Module replacement so you can edit your stores and interact with them directly in your app without reloading the page, allowing you to keep the existing state, add, or even remove state, actions, and getters.
+
+At the moment, only [Vite](https://vitejs.dev/) is officially supported but any bundler implementing the `import.meta.hot` spec should work (e.g. [webpack](https://webpack.js.org/api/module-variables/#importmetawebpackhot) seems to use `import.meta.webpackHot` instead of `import.meta.hot`).
+You need to add this snippet of code next to any store declaration. Let's say you have three stores: `auth.js`, `cart.js`, and `chat.js`, you will have to add (and adapt) this after the creation of the _store definition_:
+
+```js
+// auth.js
+import { defineStore, acceptHMRUpdate } from 'pinia'
+
+const useAuth = defineStore('auth', {
+  // options...
+})
+
+// make sure to pass the right store definition, `useAuth` in this case.
+if (import.meta.hot) {
+  import.meta.hot.accept(acceptHMRUpdate(useAuth, import.meta.hot))
+}
+```
index 4337a2f0c977db51b6efc8de915356d1d4a20fda..7ebe03cfea12f85ff8cc7b2a5640e63e0701461d 100644 (file)
@@ -1,5 +1,6 @@
 # Cookbook
 
+- [HMR](./hot-module-replacement.md): How to activate hot module replacement and improve the developer experience.
 - [Testing Stores (WIP)](./testing.md): How to unit test Stores and mock them in component unit tests.
 - [Composing Stores](./composing-stores.md): How to cross use multiple stores. e.g. using the user store in the cart store.
 - [Options API](./options-api.md): How to use Pinia without the composition API, outside of `setup()`.