Skip to content
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

watchEffect within effectScope() stops working after navigating away from then back to component #7606

Closed
franck102 opened this issue Jan 30, 2023 · 7 comments
Labels
has workaround A workaround has been found to avoid the problem 🐞 bug Something isn't working scope: reactivity

Comments

@franck102
Copy link

franck102 commented Jan 30, 2023

Reproduction

Playground SFC

Steps to reproduce the bug

  1. Navigate to the TestView component in the browser
  2. Click the button, testData count property is incremented
  3. Navigate away from it (staying in the SPA)
  4. Navigate back to TestView

Expected behavior

The button should cause testData.count to be incremented, even after re-mounting the component.

Actual behavior

After navigating back to the component clicking the button updates the refresher, but not testData

testStore.js?t=1675088280233:17 Refresher: 0.05272026601141877
testStore.js?t=1675088280233:11 Watch effect running 0.05272026601141877
testStore.js?t=1675088280233:17 Refresher: 0.937791768731258
testStore.js?t=1675088280233:11 Watch effect running 0.937791768731258
# Navigate away
logger.js:197 [Router] BeforeEach /passes
logger.js:197 [Router] AfterEach: /passes
# Then back
logger.js:197 [Router] BeforeEach /test
logger.js:197 [Router] AfterEach: /test
testStore.js?t=1675088280233:17 Refresher: 0.558079237843816
testStore.js?t=1675088280233:17 Refresher: 0.31739197899941574

@posva posva transferred this issue from vuejs/pinia Jan 30, 2023
@posva posva changed the title watchEffect stops working after navigating away from then back to component watchEffect within effectScope() stops working after navigating away from then back to component Jan 30, 2023
@posva
Copy link
Member

posva commented Jan 30, 2023

Trasfered from pinia and updated the reproduction.
This is similar to #7319 but not exactly the same. Use a regular watch() as a workaround.

@posva posva added 🐞 bug Something isn't working scope: reactivity has workaround A workaround has been found to avoid the problem labels Jan 30, 2023
@franck102
Copy link
Author

I cannot use a regular watch, as I mentioned the sample code above is an inlined version of VueUse:computedAsync.

Here is the original store:

import { defineStore } from "pinia"
import { epClient } from "@/common-services/epClient"
import { recomputedAsync } from "@/common-services/vue-recomputed-async"
import urlcat from "urlcat"

export const useRegisterStore = defineStore("RegisterStore", () => {

  function logError(e) {
    console.log("ComputedAsync error: " + JSON.stringify(e))
  }

  const { data: passes, refresh: refreshPasses } =
    recomputedAsync(async () => {
      return await epClient.get("/register/passes")
    }, [], { onError: logError })

const savePass = async (pass) => {
    try {
      await epClient...
    } finally {
      refreshPasses()
    }
  }

@franck102
Copy link
Author

The linked issue put me on the right track, calling useXxxStore from main.js before the Vue app is mounted is a valid workaround, watchEffect gets called with currentInstance = null and the effect works normally.

@edison1105
Copy link
Member

edison1105 commented Jan 31, 2023

@posva

Trasfered from pinia and updated the reproduction. This is similar to #7319 but not exactly the same. Use a regular watch() as a workaround.

I think the root cause is exactly the same and @LinusBorg 's PR will fix this. see

@posva
Copy link
Member

posva commented Jan 31, 2023

hmm, maybe something after cd7c887 broke this then? because it fails on the latest commit on the main branch

@edison1105
Copy link
Member

edison1105 commented Feb 2, 2023

@posva
duplicate of #7319
it works on the latest version. see

@posva
Copy link
Member

posva commented Feb 2, 2023

Now I see that the import maps were version-fixed and had the wrong version of Vue 🥲

@github-actions github-actions bot locked and limited conversation to collaborators Sep 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
has workaround A workaround has been found to avoid the problem 🐞 bug Something isn't working scope: reactivity
Projects
None yet
Development

No branches or pull requests

3 participants