-
-
Notifications
You must be signed in to change notification settings - Fork 986
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Store state losing reactivity when used with router Vue 2 #370
Comments
Just found the workaround, basically |
Hi, I'm glad you like it! I will need a boiled down repro with no ts, Babel, lint, tests, etc (because they have no influence) that I can run |
Alright, I have created a repo with Vue app only with router, no typescript, babel etc. you can just
Step 1: clicking the "click" button |
Oh and one more think just notice that import { defineStore } from 'pinia' // this does not exists Instead old function must be still used import { createStore } from "pinia"; |
thank you! I will check those issues out
|
I checked those issues and vuejs/rfcs#212. Does it mean that component is disposing state on the store root only? When I try to wrap root state of the store in another object everything works as expected and reactivity won't get broken. state: () => ({
innerState: { // solves the issue
counter: 0,
name: "Eduardo",
},
}), |
Fix #370 BREAKING_CHANGE: It's now necessary to create a pinia instance and install it: ```js import { createPinia } from 'pinia' const pinia = createPinia() Vue.use(pinia) ``` The `pinia` instance can be passed to `useStore(pinia)` when called outside of a `setup()` function. Check the SSR section of the docs for more details. BREAKING_CHANGE: `setActiveReq()` and `getActiveReq()` have been replaced with `setActivePinia()` and `getActivePinia()` respectively. `setActivePinia()` can only be passed a `pinia` instance created with `createPinia()`. BREAKING_CHANGE: Since req as a parameter was replacetd with `pinia`, `getRootState` is no longer necessary. Replace it with `pinia.state.value` to **read and write** the root state`. BREAKING_CHANGE: `PiniaSsr` is no longer necessary and has been removed.
Fix #370 BREAKING_CHANGE: It's now necessary to create a pinia instance and install it: ```js import { createPinia, PiniaPlugin } from 'pinia' const pinia = createPinia() Vue.use(PiniaPlugin) new Vue({ el: '#app', pinia, // ... }) ``` The `pinia` instance can be passed to `useStore(pinia)` when called outside of a `setup()` function. Check the SSR section of the docs for more details. BREAKING_CHANGE: `setActiveReq()` and `getActiveReq()` have been replaced with `setActivePinia()` and `getActivePinia()` respectively. `setActivePinia()` can only be passed a `pinia` instance created with `createPinia()`. BREAKING_CHANGE: Since req as a parameter was replacetd with `pinia`, `getRootState` is no longer necessary. Replace it with `pinia.state.value` to **read and write** the root state`. BREAKING_CHANGE: `PiniaSsr` is no longer necessary and has been removed.
Fix #370 BREAKING_CHANGE: It's now necessary to create a pinia instance and install it: ```js import { createPinia, PiniaPlugin } from 'pinia' const pinia = createPinia() Vue.use(PiniaPlugin) new Vue({ el: '#app', pinia, // ... }) ``` The `pinia` instance can be passed to `useStore(pinia)` when called outside of a `setup()` function. Check the SSR section of the docs for more details. BREAKING_CHANGE: `setActiveReq()` and `getActiveReq()` have been replaced with `setActivePinia()` and `getActivePinia()` respectively. `setActivePinia()` can only be passed a `pinia` instance created with `createPinia()`. BREAKING_CHANGE: Since req as a parameter was replacetd with `pinia`, `getRootState` is no longer necessary. Replace it with `pinia.state.value` to **read and write** the root state`. BREAKING_CHANGE: `PiniaSsr` is no longer necessary and has been removed.
Fixed in #379 |
Fix #370 BREAKING CHANGE: It's now necessary to create a pinia instance and install it: ```js import { createPinia, PiniaPlugin } from 'pinia' const pinia = createPinia() Vue.use(PiniaPlugin) new Vue({ el: '#app', pinia, // ... }) ``` The `pinia` instance can be passed to `useStore(pinia)` when called outside of a `setup()` function. Check the SSR section of the docs for more details. BREAKING CHANGE: `setActiveReq()` and `getActiveReq()` have been replaced with `setActivePinia()` and `getActivePinia()` respectively. `setActivePinia()` can only be passed a `pinia` instance created with `createPinia()`. BREAKING CHANGE: Since req as a parameter was replacetd with `pinia`, `getRootState` is no longer necessary. Replace it with `pinia.state.value` to **read and write** the root state`. BREAKING CHANGE: `PiniaSsr` is no longer necessary and has been removed.
Reproduction
Hello, first of all I would like to say that I love pinia and this direction and I'm actually trying to use it in production with Vue2 and composition-api library.
I have an issue which I would like to help with. Issue can be easily reproduced with new vue-cli project, router with history mode, Typescript and Babel without class components.
Install composition-api and pinia for Vue2 and register composition-api in main.ts by
Vue.use()
.Next just need two component, store from documentation here on github and router.
stores/test-store.ts
views/MainComponent.vue
views/SecondComponent.vue
router/index.ts
After setting up the app there are to buttons, go to next route button and click button, there is also logic in MainComponent.vue setup() function to increment counter every time and to console.log() to make sure setup function is being called.
When you serve the app everything works fine, counter gets automatically incremented to 1 and when you click on button it keeps incrementing. However when you get to the second route using go button and back using history back button, setup function is triggered but counter is not incremented and click button also not incrementing anymore. Event of the click button is being fired just reactivity of store state seems broken. (even if you not use browser back history btn but setup router-link in the next component and redirect back it's gonna be the same).
Any ideas what could be the issue and potential workaround?
Thank you very much for help!
Screen.Recording.2021-02-25.at.4.13.52.PM.mov
The text was updated successfully, but these errors were encountered: