Hello everyone, apologies for such a noob questions but while learning and playing with the Apollo React Client I keep having an error that I’m not sure how to handle.
Hi @SGthatGui welcome to the community forum! React Server Components are stateless so any React hooks that contain hooks like useState and useContext can only be used on client components. That includes parts of the Apollo Client React API including ApolloProvider and hooks like useQuery. Hope that helps!
One thing to think about here is that using SSR and server components is that there is no cache (@apollo/client’s cache doesn’t matter here because things are loading only once on the server - stateless).
I can think of three options you have here in the short term if you really want to use React server components today:
If you don’t use ApolloProvider and instead pass the ApolloClient instance to useQuery directly:
const client = new ApolloClient({...});
...
const result = useQuery(MY_QUERY,{client});
Use the query or mutate methods available on ApolloClient:
const client = new ApolloClient({...});
...
const result = await client.query({ query: MY_QUERY });
Use something like urql for your server components and @apollo/client for the rest of your app.
I hope this helps and if you want to talk more about it live, we have a Discord server that you can join. Feel free to ping me (@watson)!
let me begin for thanking you for your time and attention
Will think about it and test out the different solutions you have depicted, good to stumble on these noob problems now as a lot of js frameworks are adopting new render and hydration methods, this is also my first contact with graphQL and Apollo still a lvl0 noob