Use <Suspense /> on the client only
Answered
Himalayan posted this in #help-forum
HimalayanOP
I try to play with suspense and react-query@v5.
The fetch should be done in the client only because it access the local storage
In the server, I have an error telling me local storage is not defined (I'm ok with that), but I don't understand why is my "use client" is executed server side, and how can I disable this?
thanks
The fetch should be done in the client only because it access the local storage
async function getData() {
const token = localStorage.getItem("token")
await fetch('/xxxxxxxxx', { ... })
return ...;
}
function useData() {
return useSuspenseQuery({
queryKey: ["data"],
queryFn: () => getData();
});
} "use client";
function SuspenseWrapper() {
return (<Suspense><SuspenseChildren /></Suspense>)
}
// other file with "use client";
function SuspenseChildren() {
const { data } = useData();
return <div> { ... } </div>
}In the server, I have an error telling me local storage is not defined (I'm ok with that), but I don't understand why is my "use client" is executed server side, and how can I disable this?
thanks
Answered by Himalayan
Ok, I figured it out, my problem was not RSC but SSR, I loaded my components with
dynamic and {Â ssr: false } and the errors have gone1 Reply
HimalayanOP
Ok, I figured it out, my problem was not RSC but SSR, I loaded my components with
dynamic and {Â ssr: false } and the errors have goneAnswer