-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
React query not working with astro file #11499
Comments
Can you please the reproduction or the PR description? The reproduction isn't using |
Hello @pycanis. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with |
The reproduction was using a library ( https://stackblitz.com/edit/github-njjsdv-zcx9t1?file=src%2Fpages%2Findex.astro
|
Is there anything I can help with? I'd really appreciate having this looked into 🙏 |
I spent some time triaging the issue, and I believe this is expected, considering the way you're setting up your project. Removing <QueryClientProvider client:only="react" client={queryClient}>
<NotWorking />
</QueryClientProvider> You have to understand that In this particular example, you're creating Closing because this is working as expected. Please, next time, reach out to Discord. |
I'm not really sure. I've updated the reproduction with the wrapper around https://stackblitz.com/edit/github-njjsdv-zcx9t1 Shall I move the discussion to discord? |
Yes please |
Sorry for bothering you again but I'm not getting much help on discord. I've made the reproduction in the simplest way possible, could you have a look please? It seems like the issue is with the https://stackblitz.com/edit/github-njjsdv-zcx9t1 Could you try to explain to me why "THIS IS NOT WORKING" string is not rendered in the UI? |
@pycanis a couple issues I am seeing with your StackBlitz:
Your Wrapper needs to be around React components only. Once you add a React component any children must also be React. This makes sense because the Astro components are on the server and then the client-side hydration for reactive components happens down the component tree. If you place a server component in the middle of that — in this case your |
@joshuaiz thank you for clearing a few things up for me. Is there currently a way to have an Astro island that uses React Context to not re-render the context on page change, only the content inside of the context? I've updated the StackBlitz again to demonstrate. https://stackblitz.com/edit/github-njjsdv-zcx9t1 I think it's obvious that the My goal is to NOT see |
Hi @pycanis this is always a challenge with context + providers. Depends on what you are querying and how often you need to refresh the data but there should be a way to use Here's an example: https://medium.com/@trisianto/react-query-how-to-memoize-results-from-usequeries-hook-eaed9a0ec700 |
It's not about the query results. I just need the same Astro island to use the same react context ( I'm starting to feel like it's not possible with Astro although this is a fairly common use case.. |
Got it. I think the main point is there is no "Context" with Astro - there's no way to wrap Astro components like that. But, you can add your Context/Wrapper at the top level or anywhere else...consider these two examples: <Layout><!-- Astro -->
<Wrapper client:load><!-- React from here down -->
{children}<!-- React children -->
</Wrapper>
</Layout> ^^ Here we are adding the wrapper high in the component tree. <Layout><!-- Astro -->
<AstroComponent>
<Wrapper client:load><!-- React from here down -->
{children}<!-- React children -->
</Wrapper>
<AstroComponent>
<AstroComponent>
<slot /><!-- Astro children -->
<Wrapper client:load><!-- React from here down -->
{children}<!-- React children -->
</Wrapper>
</AstroComponent>
</AstroComponent>
<AnotherAstroComponent>
<slot /><!-- Astro children -->
</AnotherAstroComponent>
</AstroComponent>
</Layout> ^^ Here we are adding the first wrapper high-ish and then another wrapper further down. We also have slots that are only between Astro components. Note that the two wrappers will have their own context(!). You can't wrap your entire app in a provider like with pure React or share context across Astro islands. |
One more point to be made here: if you don't need the cache invalidation and re-fetching aspects of TanStack Query, you can use Astro's top-level fetch + await, fetch the data on the server in Astro and pass it down to React components if you need reactivity or in Astro if you don't: <!-- Astro component -->
---
import Layout from '/layout/Layout.astro';
const response = await fetch('https://api.example.com/data');
const data = await response.json();
---
<Layout>
<ReactComponent data={data} client:load />
<!-- OR use the data in Astro -->
{data.map((item) => (
<div>{item.name}</div>
))}
<!-- OR pass down to another Astro component -->
</Layout> The point is: you don't need React to fetch data and fetching it on the server means the data is already ready for your components. https://docs.astro.build/en/guides/data-fetching/ |
Okay, I understand the component nesting pattern. It makes sense and what I need works with that. My main problem is that navigating between pages re-renders all the components, not only ones that changed. I admit to be "thinking in react" terms which probably doesn't suit the Astro concepts. Regarding the data fetching - I need to be fetching on the client because I'm fetching from a local SQLite database. Essentially I just need a part of the application that lives on Is there maybe a way to not create Astro page for every dashboard page but only the index and let the client side react take over the routing there? Probably not going to work for hard refresh e.g. on Sorry for pushing hard in this direction. Astro is like a breath of fresh air (coming from Nextjs) so I took a week to rewrite my app to use Astro only to learn the last piece of puzzle missing. |
Hi @pycanis! Running into this issue as well. Seems like its an intended side effect from astro but I wanted how you structured the solution for this. Thanks so much in advance! edit: followed this link that you sent but doesn't seem to work from me: <https://stackblitz.com/edit/github-njjsdv-zcx9t1 |
Hey @GabrielPedroza, have a look here. The solution is having the whole SPA under single astro page. More specifically here and here. Hope that helps. |
Thanks for the quick reply! Just to make sure I understand, you are basically just adding a bunch of routes that will use react query to a rootRouter in a singular place (App.tsx) and you will load the corresponding component depending on the url param. So if you are in |
React query is loaded just once when you navigate to any of the That way, you benefit from having static pages along with full fledged client side app. |
Astro Info
If this issue only occurs in one browser, which browser is a problem?
No response
Describe the Bug
Importing a React library directly in astro layout file results in
No QueryClient set, use QueryClientProvider to set one
error coming from@tanstack/react-query
.If I create a new React component that uses the library and is then imported in astro layout file, it's working fine.
Have a look at a reproducible example.
What's the expected result?
Importing a react library in astro layout file directly to be working with
@tanstack/react-query
.Link to Minimal Reproducible Example
https://stackblitz.com/edit/github-njjsdv?file=src%2Flayouts%2FLayout.astro
Participation
The text was updated successfully, but these errors were encountered: