-
-
Notifications
You must be signed in to change notification settings - Fork 48
allow using client in effectScope #203
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
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 4048311:
|
|
Kudos, SonarCloud Quality Gate passed! |
|
Hi, Would you mind explaining what is the issue you are encountering without this fix? Ex. you are getting some kind of error? Minimal reproduction example would be helpful to narrow down the issue and find more general solution. |
|
This example doesn't prove the need for effect scopes but demonstrates that it doesn't work. <script setup lang="ts">
import {effectScope, EffectScope, onScopeDispose, ref, watch, watchEffect} from "vue";
import {useQuery} from "vue-query";
const sources = ref(5);
const newData = ref<string>();
setTimeout(() => sources.value = 1, 1000);
let scope: EffectScope | undefined;
watch(sources, () => {
scope?.stop();
scope = effectScope();
scope.run(() => {
const { data } = useQuery(["todos"], () => "hello world");
watchEffect(() => newData.value = data.value)
});
});
onScopeDispose(() => scope?.stop());
</script>
<template>
<h1>{{ newData }}</h1>
</template>here is the first error For the most part using watch, computed etc is fine to do in an effectScope and the error check could be changed to the following. if (!getCurrentScope()) {
throw new Error(
"vue-query hooks can only be used inside setup() function."
);
}But this results in the following error Inject assumes that the injected key is provided somewhere in the parent chain. My understanding is that useEffect doesn't have a parent scope meaning it won't find the provided data. I'm wondering if it would be ok to store the queryClients in a global object instead of using vue's inject provide? |
|
So, skipping the fact that i do not see/understand the real use-case of enclosing let query;
const scope = effectScope();
scope.run(() => {
query = reactive(useQuery(["posts"], fetcher));
});
return { query };It does not work, when
In case you want to hack your way to make |
|
I agree that this pull request is not elegant and I have not presented a valid example where it is needed. From my perspective if people use effect scopes they might want to use In our project we have an array of refs that sometimes changes based on user input or a change on the backend. When that happens we create a new effect scope and setup a few new watchers and computed values and stop the old effect scope. I would love to use |
|
Here's a different approach of getting the query client from within effect scope outside of setup. |
|
So, i have thought about this and i guess that this can cause problem when you will have multiple applications instances on the page, but you do not want to share the instance between those. I think what we could do to eventually support this is to add ability to pass This way you would be able to Ofc we would need to do the same modification in all composables. |
|
Kudos, SonarCloud Quality Gate passed! |
|
@DamianOsipiuk I updated the pull to allow passing in the query client. I simply searched for places useQueryClient was used and updated them to first look for I was considering renaming type Another thought I had was to merge |
Codecov ReportBase: 100.00% // Head: 100.00% // No change to project coverage 👍
Additional details and impacted files@@ Coverage Diff @@
## beta #203 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 14 14
Lines 377 378 +1
Branches 64 69 +5
=========================================
+ Hits 377 378 +1
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
|
@ant32t Thanks for the contribution! 🚀 |
|
🎉 This PR is included in version 2.0.0-beta.11 🎉 The release is available on: Your semantic-release bot 📦🚀 |








I would like to be able to use effectScope