Next.js Discord

Discord Forum

Optimizing React Data Fetching with Suspense, React Query, and Next.js: Seeking Feedback on Architec

Unanswered
MatheusDeveloper posted this in #help-forum
Open in Discord
I'm developing a project using a specific architectural pattern and would like to share my approach to gather feedback. The architecture focuses on React components, Suspense for loading state management, React Query for data management, and an internal Next.js API for database communication via Supabase. Here’s an overview of the flow:

Server Component with React Query: The entry point is a server component where I use React Query to prefetch user data with the prefetchQuery function. This function calls getUser, a service defined in another file which, in turn, makes a request to an internal API.

getUser Service: The getUser service interacts with the internal API by sending an HTTP GET request to fetch user data. This request is processed by a specific endpoint within the Next.js API folder, which queries the Supabase database based on the user's email, returning the corresponding data.

Suspense and Message Component: In the server component, I wrap the call to the Message component within a <Suspense>, providing a visual fallback during data loading. The Message component uses the useGetUser hook to access user data.

useGetUser Hook: This custom hook leverages React Query to fetch data, calling the getUser function again. It enables efficient management of loading and error states, as well as reusing the data access logic across components.

The goal of this architecture is to optimize data loading and user experience, minimizing wait times and improving code efficiency. I'm seeking opinions on the effectiveness of this approach, possible improvements, and any other recommended strategies that could enrich this development pattern.

https://whimsical.com/fluxo-de-interacao-de-componentes-VaknL9da7czJim2PmkTdQd?ref=chatgpt

0 Replies