Do you need to use Suspense when using async React Server Component inside client component in Next.js?

I’m learning Next.js, and I’m trying to render Server async Component inside client Component:

Gallery.tsx

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { get_images } from '@/utils';
import styles from './Gallery.module.css';
export default async function Gallery() {
const images = await get_images();
return (
<ul className={styles.gallery}>
{images.map(file => {
const url = `https://example.com/images/${file}`;
return (
<li key={file}>
<span>
{/* when using Image got a double error */}
<img src={url} alt="image" />
</span>
</li>
);
})}
</ul>
);
}
</code>
<code>import { get_images } from '@/utils'; import styles from './Gallery.module.css'; export default async function Gallery() { const images = await get_images(); return ( <ul className={styles.gallery}> {images.map(file => { const url = `https://example.com/images/${file}`; return ( <li key={file}> <span> {/* when using Image got a double error */} <img src={url} alt="image" /> </span> </li> ); })} </ul> ); } </code>
import { get_images } from '@/utils';
import styles from './Gallery.module.css';

export default async function Gallery() {
  const images = await get_images();
  return (
    <ul className={styles.gallery}>
      {images.map(file => {
        const url = `https://example.com/images/${file}`;
        return (
          <li key={file}>
            <span>
              {/* when using Image got a double error */}
              <img src={url} alt="image" />
            </span>
          </li>
        );
      })}
    </ul>
  );
}

page.tsx

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>'use client';
import { useRef } from 'react';
import { revalidatePath } from 'next/cache'
import styles from "./page.module.css";
import { upload } from '@/git';
import Gallery from '@/components/Gallery';
export default function Home() {
const formRef = useRef();
async function uploadAction(formData: FormData) {
await upload(formData);
revalidatePath('/');
}
return (
<main className={styles.main}>
{/* <Gallery /> */}
<h2>Upload form</h2>
<div className={styles.form}>
<form action={uploadAction}>
</form>
</div>
</main>
);
}
</code>
<code>'use client'; import { useRef } from 'react'; import { revalidatePath } from 'next/cache' import styles from "./page.module.css"; import { upload } from '@/git'; import Gallery from '@/components/Gallery'; export default function Home() { const formRef = useRef(); async function uploadAction(formData: FormData) { await upload(formData); revalidatePath('/'); } return ( <main className={styles.main}> {/* <Gallery /> */} <h2>Upload form</h2> <div className={styles.form}> <form action={uploadAction}> </form> </div> </main> ); } </code>
'use client';
import { useRef } from 'react';
import { revalidatePath } from 'next/cache'

import styles from "./page.module.css";
import { upload } from '@/git';
import Gallery from '@/components/Gallery';

export default function Home() {
  const formRef = useRef();

  async function uploadAction(formData: FormData) {
    await upload(formData);
    revalidatePath('/');
  }
  
  return (
    <main className={styles.main}>
      {/* <Gallery /> */}
      <h2>Upload form</h2>
      <div className={styles.form}>
        <form action={uploadAction}>
        </form>
      </div>
   </main>
  );
}

I’m in a process of refactoring, I wanted to add reset to the form with useRef, so I added use client. After I extracted the Gallery into another component, started to get error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>app-index.js:33 Warning: async/await is not yet supported in Client Components, only Server Components. This error is often caused by accidentally adding `'use client'` to a module that was originally written for the server.
at Gallery
at main
at Home (webpack-internal:///(app-pages-browser)/./src/app/page.tsx:23:66)
</code>
<code>app-index.js:33 Warning: async/await is not yet supported in Client Components, only Server Components. This error is often caused by accidentally adding `'use client'` to a module that was originally written for the server. at Gallery at main at Home (webpack-internal:///(app-pages-browser)/./src/app/page.tsx:23:66) </code>
app-index.js:33 Warning: async/await is not yet supported in Client Components, only Server Components. This error is often caused by accidentally adding `'use client'` to a module that was originally written for the server.
    at Gallery
    at main
    at Home (webpack-internal:///(app-pages-browser)/./src/app/page.tsx:23:66)

If I add “use server’` to Gallery, I got this error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>app-index.js:33 Warning: Cannot update a component (`Router`) while rendering a different component (`proxy`). To locate the bad setState() call inside `proxy`, follow the stack trace as described in https://reactjs.org/link/setstate-in-render
at proxy
at main
at Home (webpack-internal:///(app-pages-browser)/./src/app/page.tsx:23:66)
</code>
<code>app-index.js:33 Warning: Cannot update a component (`Router`) while rendering a different component (`proxy`). To locate the bad setState() call inside `proxy`, follow the stack trace as described in https://reactjs.org/link/setstate-in-render at proxy at main at Home (webpack-internal:///(app-pages-browser)/./src/app/page.tsx:23:66) </code>
app-index.js:33 Warning: Cannot update a component (`Router`) while rendering a different component (`proxy`). To locate the bad setState() call inside `proxy`, follow the stack trace as described in https://reactjs.org/link/setstate-in-render
    at proxy
    at main
    at Home (webpack-internal:///(app-pages-browser)/./src/app/page.tsx:23:66)

All problems are solved when adding React.Suspense:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> <Suspense fallback={<p>loading...</p>}>
<Gallery />
</Suspense>
</code>
<code> <Suspense fallback={<p>loading...</p>}> <Gallery /> </Suspense> </code>
      <Suspense fallback={<p>loading...</p>}>
        <Gallery />
      </Suspense>

So the question is, is Suspense a must when using async server components? I only what to know what is allowed, by asking this I don’t care if it will freeze until async action finish.

I was asking ChatGPT, and it first gives answer that you can just include Server component inside Client component. When I asked about Suspense, he only mentioned a delay. I’m asking about cryptic errors when not using Suspense.

This looks like pretty common, but I’m not able to find anything how this works.

Importing a server component in a client component is not a valid pattern in Next.js.

The correct approach would be to pass server components to client components as props and, generally speaking, moving client components down your component tree.

So you don’t need to use Suspense (because you should be avoiding importing server components in client components entirely).
In a way, by using Suspense you are “tricking” Next.js into not throwing an error it should actually throw.

There are many approaches you can take for this specific scenario. The easiest in your case would probably be just keeping your homepage and gallery as regular server components, and extracting your form as a client component instead: then you could use useRef in it, and just import the new component in your homepage (which is the “moving the client component down the tree” approach).

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật