How to Fix Async Component Errors in NextJS 13+ with React 18

Easy solution that needs a revisit to Next docs

If you’re working with Next.js 13+ and React 18, you might encounter an error when trying to render an async Server Component. This happens because, in React, components that return promises (async components) require explicit handling. Here’s a step-by-step guide to fix this issue:

Understanding the Problem

In Next.js 13+, with the introduction of React Server Components, async components can be directly rendered in server-side code. However, these components must:

1. Be explicitly declared as async.

2. Handle TypeScript errors for server-side async components properly, as TypeScript support for these is still evolving.

If you don’t follow these conventions, you’ll likely face errors like:

“TypeError: Cannot read properties of undefined” when rendering the async component.

• TypeScript complaints about unexpected Promise types or server-side components.

Fixing the Issue

1. Mark the Component as async

In this case, the ProductViewPage component is async, meaning it contains asynchronous logic like data fetching (e.g., calling an API or database).

When rendering such components inside another component or page, make sure the parent is also declared as async. Here’s an example:

// Mark the Page component as async

export default async function Page() {

    return (

        <div>

            {/* Render the async component */}

            <ProductViewPage />

        </div>

    );

}typesc

2. Use the @ts-expect-error Comment

TypeScript currently has limited support for React Server Components (RSCs), especially when they’re asynchronous. To avoid TypeScript errors when rendering these components, add the following comment above the component:

export default async function Page() {

    return (

        <div>

            {/* @ts-expect-error Async Server Component */}

            <ProductViewPage />

        </div>

    );

}

This tells TypeScript to ignore the async nature of ProductViewPage temporarily. Although this solution is not ideal, it is the recommended approach by the Next.js team until TypeScript adds better support for React Server Components.

Why These Fixes Work

Async Parent Component: In React, any component that renders an async component must itself be declared as async. This ensures proper promise handling at runtime.

@ts-expect-error Comment: This bypasses TypeScript’s type-checking error for async server components. It’s a stopgap measure that prevents unnecessary type conflicts.

Summary

When working with async Server Components in Next.js 13+, remember to:

1. Mark components as async to ensure proper rendering.

2. Use @ts-expect-error comments for temporary TypeScript compatibility with React Server Components.

While this approach might seem like a workaround, it aligns with the best practices recommended by the Next.js team and ensures your app works as intended without TypeScript-related roadblocks. Keep an eye on future updates to React and TypeScript for improved support!

Did you find this article valuable?

Support The Nerdy Nomad by becoming a sponsor. Any amount is appreciated!