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

How do I deconstruct multiple levels within a useSanityQuery() variable #566

Open
toddpadwick opened this issue Nov 17, 2022 · 0 comments
Open
Labels
question Further information is requested

Comments

@toddpadwick
Copy link

Hello!

I have a single GROQ query containing three sub-queries:

{
    'entry':*[_type == "productIndex"][0]{
        ...
    }
    'gender':*[_type == "gender" && slug.current == "women"][0]{
	...
    }
    'collection':*[_type == "collection" && store.slug.current == "t-shirts"][0]{
	... 
    }
}

I have set up the following query:

    const { pending, data, error } = useSanityQuery(query);

This returns the following:
Screenshot 2022-11-17 at 17 41 57

This works however, to get access to the containing gender, entry and collection as their own refs, I would like to deconstruct the data. I have tried this:

    const { pending, data:{entry,collection,gender}, error } = useSanityQuery(query);

I thought this would work but it returned null, and I expect this is because they are proxy values. So how best can I achieve this?

I have tried, setting refs initially and then assigning them after the query is complete, however, this causes race conditions and intermittently catches errors. it is also inefficient as it means there is a ref within a ref:

   let entry = ref(null);
  let gender = ref(null);
  let collection = ref(null);

    const { pending, data, error } = useSanityQuery(query);


if (data) {
    entry.value = data.value?.entry ? data.value.entry : null;
    gender.value = data.value?.gender ? data.value.gender : null;
    collection.value = data.value?.collection ? data.value.collection : null;
}
 

Thanks in advance for your help :)

@toddpadwick toddpadwick added the question Further information is requested label Nov 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant