Next.js Discord

Discord Forum

Best Way to Fetch in a Client Component in Next.js 14: useEffect with Route Handlers vs Server Actio

Answered
Miniature Schnauzer posted this in #help-forum
Open in Discord
Miniature SchnauzerOP
I'm working on a Next.js 14 project and I need to fetch data in a client component. I've come across two different methods and I'm unsure which one is the best practice.

### Method 1: useEffect with an Async Function and Server Action

In this method, I use useEffect with an async function to fetch data directly from a server action that I also use in my server components. This way, I can reuse my server actions for both fetching and mutating data.

### Method 2: useEffect with Route Handlers

In this method, I set up an API route using a Route handler and fetch the data within useEffect by making a call to this route. According to the Next.js documentation, this seems to be the recommended way for fetching data in client components.

### My Question

I prefer using the first method because it allows me to reuse server actions for both fetching and mutating data. However, the Next.js documentation states that server actions are primarily for mutating data and not for fetching data in client components. It also recommends using Route handlers for fetching data.

- What is the best practice for fetching data in a client component in Next.js 14?
- Is it acceptable to use server actions within useEffect for data fetching in a client component, or should I strictly use Route handlers for this purpose?
- Are there any performance or architectural considerations I should be aware of when choosing between these methods?

https://stackoverflow.com/questions/78600421/best-way-to-fetch-data-in-a-client-component-in-next-js-14-useeffect-with-route
Answered by joulev
What is the best practice for fetching data in a client component in Next.js 14?
route handlers with data fetching libraries, such as react-query or swr.

Is it acceptable to use server actions within useEffect for data fetching in a client component, or should I strictly use Route handlers for this purpose?
no, see below. it's best to use data fetching libraries, but if you really want you can still use useEffect – but route handlers are required. why? see below.

Are there any performance or architectural considerations I should be aware of when choosing between these methods?
server actions cannot be run in parallel, so that completely disqualifies them from being a good data querying method.
View full answer

6 Replies

@Miniature Schnauzer I'm working on a Next.js 14 project and I need to fetch data in a client component. I've come across two different methods and I'm unsure which one is the best practice. ### Method 1: `useEffect` with an Async Function and Server Action In this method, I use `useEffect` with an async function to fetch data directly from a server action that I also use in my server components. This way, I can reuse my server actions for both fetching and mutating data. ### Method 2: `useEffect` with Route Handlers In this method, I set up an API route using a Route handler and fetch the data within `useEffect` by making a call to this route. According to the Next.js documentation, this seems to be the recommended way for fetching data in client components. ### My Question I prefer using the first method because it allows me to reuse server actions for both fetching and mutating data. However, the Next.js documentation states that server actions are primarily for mutating data and not for fetching data in client components. It also recommends using Route handlers for fetching data. - **What is the best practice for fetching data in a client component in Next.js 14?** - **Is it acceptable to use server actions within `useEffect` for data fetching in a client component, or should I strictly use Route handlers for this purpose?** - **Are there any performance or architectural considerations I should be aware of when choosing between these methods?** https://stackoverflow.com/questions/78600421/best-way-to-fetch-data-in-a-client-component-in-next-js-14-useeffect-with-route
What is the best practice for fetching data in a client component in Next.js 14?
route handlers with data fetching libraries, such as react-query or swr.

Is it acceptable to use server actions within useEffect for data fetching in a client component, or should I strictly use Route handlers for this purpose?
no, see below. it's best to use data fetching libraries, but if you really want you can still use useEffect – but route handlers are required. why? see below.

Are there any performance or architectural considerations I should be aware of when choosing between these methods?
server actions cannot be run in parallel, so that completely disqualifies them from being a good data querying method.
Answer
Netherland Dwarf
Also isnt it much slower to do 2 different request
Vs just 1 request
Sending request to api then in the api send another request whereas just do 1 request using react query
you're welcome